2014-02-08 51 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 !!!!!!!!!!我們快到了...... –