2014-02-08 53 views
0

确定注入的角度可变进一个过滤器从NG-重复,让我再试试这个...如何使用underscore.js

我有一个值从纳克重复来了...是这样的:

<div class='span3 box box-nomargin' style="margin-left: 0px !important; padding-right: 20px;" ng-repeat="site in getSites"> 

这是“site.networkId”的值是我必须得到网络的名称。

这就是说,网络ID是:“2352wdfdsf23214”例如。

名称是另一种JSON数组...所以我想这个值传递到过滤器对于角度是这样的:

<span class="utilMonSmallText">{{ name | netNames:site.networkId }}</span> 

是最终的价值,我需要和网络名它过滤器和site.networkId是我从上面的ng重复的论点。

因此,这里是我的过滤器:

myApp.filter('netNames', function(myNetID) 
{ 
    console.log("Inside NetNames Filter: " + myNetID); 

    var networkName = _.filter(myNetID, function(name){ return name ==="name"; }); 

     return networkName; 
}); 

所以,如果我有车把,我想简单地创建一个帮助和噗,所需要的结果将呈现。角;不那么容易,或者我没有看到它。

网络集合具有ID和NAME。我只是想用传递给GET的名称替换名称。

简单;对?

更新:这是我的控制器......

// getNetNames gets an array with all the network names in the database 
$scope.getNetNames = dashboardData.getNetNames(); 
$scope.getNetNames.then(
     function(getNetNames) { 

       $scope.getNetNames = getNetNames; 
       $scope.netName = $.grep(getNetNames, function(n, i) { // just use arr 

        console.log("myName inside the topo getNetNames: " + n.myNetID) 

        return n.myNetID === "NAME"; 
       }); 

      console.log("get Names inside TOPO Ctrl: " + $scope.getNetNames); 
     }, 
     function(response) { 
      console.log(response); 
     }); 

更新:这是我的服务

getNetNames: function(netid) { 
     var deferred = $q.defer(); 
      $http({method: 'GET', url: myNetsURL + "" + netid}) 
        .success(function(data, status, headers, config) { 
         console.log("NETWORK Name: " + data); 
         //Push NET NAMES into array for use down below. 
         deferred.resolve(data); 
        }) 
        .error(function(data, status, headers, config) { 
         deferred.reject(status); 
        }); 
     return deferred.promise; 
    } 

回答

2

你不及格site.networkId到过滤器的正确方法:

myApp.filter('netNames', function() { // You can inject a service here 
    return function(input, myNetID) { // the first parameter refers to "name" which is the json array that has the names 
     console.log("Inside NetNames Filter: " + myNetID); 

     var networkName = _.findWhere(input, {id: myNetID}); 

     return networkName.name; 
    } 
}); 

这假设具有名称的json数组在$scope.name

+0

Ranru - 这就是呈现的内容: []

+0

untiMonSmallText是我的CSS元素。 “[]”很奇怪......但是,我没有收到任何错误......只是“未定义”网络“ID”应该是(即参数:myNetID) –

+0

OMG - 你说的INPUT参数是“未定义”不是。它包含NETWORK ID !!!!!!!!!!我们快到了...... –