如何在 angular 1 中一起使用 ng-if 和 ng-options

How to use ng-if and ng-options together in angular 1

我使用以下语法来显示用户名。我想在下面的列表中 (user.company==userData.company) 时显示用户名。请帮我实现这个目标。

<select ng-options="user.FirstName+' 'user.LastName for user in companyUsers | orderBy:'FirstName' track by user.Id" ng-if="user.company==userData.company" ng-model="selectedUser" ng-change="loadListNew()">
</select>

您的代码有一个错误:user.FirstName + ' ' + user.LastName,您忘记了第二个 +

更新代码:

<select ng-options="user.FirstName + ' ' + user.LastName for user in companyUsers | orderBy:'FirstName' track by user.Id" 
    ng-if="user.company==userData.company"
    ng-model="selectedUser"
    ng-change="loadListNew()">
</select>

但问题是 ng-if 不适用于 ng-options,因为 ng-options 不会创建新范围。

您应该做的是创建一个过滤器,从输入中删除数据,因此 ng-options 将跳过它。您可以在 orderBy-过滤器之前或之后应用它。

使用 ng-if="user.company==userData.company" 不起作用,因为它将应用于 select 元素。

过滤器选项

相反,您可以直接在 ng-options 中使用过滤器,例如 example.

过滤文档

https://docs.angularjs.org/api/ng/filter/filter

    You may move your logic to controller rather than in html. You may filter data $scope.companyUsers before bind it to select options. 

    $scope.companyUsers =  // where you assign the value to companyUsers of scope variable. below this code do something like
    $scope.companyUsers = $scope.companyUsers.map( function(companyUserObj){
     if(companyUserObj.company==$scope.userData.company)
    {
       return companyUserObj;
    }

    })
now bind this $scope.companyUsers to select options as below.
<select ng-options="user.FirstName+' '+user.LastName for user in companyUsers | orderBy:'FirstName' track by user.Id" ng-model="selectedUser" ng-change="loadListNew()">
</select>