0

我正在尝试将 DOM 元素的模型设置为给定特定属性的特定子对象。在此特定示例中,“Employees”对象的结构如下:

var Employees = [
    {EmployeeID:1000
     Name: Jimmy
     ManagerID: null}
    {EmployeeID:1001
     Name: James
     ManagerID: 1000}
    {EmployeeID:1002
     Name: Steve
     ManagerID: 1000}

] 

我希望使用过滤器的内联 Curley Brace 指令能够帮助我解决它。接下来的内容显然不起作用,但希望它证明了我想要它做的事情。

<div ng-repeat="Emp in Employees">
        <span>Employee #:{{Emp.EmployeeID}}</span>
        <img src="../../Content/images/Photos/{{Emp.EmployeeID}}.jpg"/>
        <h4>{{Emp.Name}}</h4>
        <h5>Manager:</h5>
        <!--The following is what I do not know how to do-->
        <span>{{Employees.Name | filter:Employees.EmployeeID = Emp.EmployeeID }}</span>
</div>

最终结果是:

Employee #:1000
<img>
Jimmy
Manager:
<!--I'll figure out what to do with null values in a bit.  first things first-->

Employee #:1001
<img>
James
Manager:
Jimmy

Employee #:1002
<img>
Steve
Manager:
Jimmy
4

1 回答 1

1

JS

angular.module("myApp", []).controller("MyCtrl", function($scope){
    $scope.employees = [
        {EmployeeID:1000,
         Name: 'Jimmy',
         ManagerID: null},
        {EmployeeID:1001,
         Name: 'James',
         ManagerID: 1000},
        {EmployeeID:1002,
         Name: 'Steve',
         ManagerID: 1000}
    ]

    $scope.findManager = function(empId) {
      console.log('here');
        //debugger;
        for(var i=0; i<$scope.employees.length; i++)
        {
            var curEmployee = $scope.employees[i];
            console.log(curEmployee.EmployeeID)
            if(curEmployee.EmployeeID == empId)
                return curEmployee.Name;
        }
        return "";
    }
});

的HTML

<div ng-app="myApp" ng-controller="MyCtrl">
      <div ng-repeat="Emp in employees">
        <span>Employee #:{{Emp.EmployeeID}}</span>
        <img ng-src="../../Content/images/Photos/{{Emp.EmployeeID}}.jpg"/>
        <h4>{{Emp.Name}}</h4>
        <h5>Manager:</h5>
        <!--The following is what I do not know how to do-->
        <span>{{findManager(Emp.ManagerID)}}</span>
      </div>
    </div>

小提琴

http://jsfiddle.net/EhJLu/

于 2013-07-19T16:57:53.080 回答