Javascript 如何使用下划线.js将角度变量从ng repeat注入过滤器
好的,让我再试一次 我有一个来自ng repeat的值。。。像这样:Javascript 如何使用下划线.js将角度变量从ng repeat注入过滤器,javascript,angularjs,underscore.js,Javascript,Angularjs,Underscore.js,好的,让我再试一次 我有一个来自ng repeat的值。。。像这样: <div class='span3 box box-nomargin' style="margin-left: 0px !important; padding-right: 20px;" ng-repeat="site in getSites"> <span class="utilMonSmallText">{{ name | netNames:site.networkId }}</span&g
<div class='span3 box box-nomargin' style="margin-left: 0px !important; padding-right: 20px;" ng-repeat="site in getSites">
<span class="utilMonSmallText">{{ name | netNames:site.networkId }}</span>
因此,如果我有把手,我只需创建一个辅助对象并poof,就可以呈现所需的结果。有棱角的没那么容易,也许我没看到
网络集合具有ID和名称。我只想用传递的ID替换名称以获取名称
简单;对吧?
更新:这是我的控制器
// 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;
}
您没有以正确的方式将
站点.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
Ranru中-这就是要呈现的内容:[]untiMonSmallText是我的CSS元素。“[]”很奇怪。。。但是,我没有得到任何错误。。。只是“未定义”,其中网络“ID”应该是(即参数:myNetID)OMG-您所说的输入参数“未定义”不是。它包含网络ID!!!!!!!!!!我们就快到了,id
现在可以在您的过滤器中使用了。问题在于您的下划线JS函数。我不知道名为的json数组是什么样子的。给你:{“id”:“f2a66122-927f-4657-98b3-b031afecff69”,“networkType”:1,“controllerIp”:“10.7.12.1”,“redirectType”:1,“networkCost”:0,“networkWeight”:0,“notes”:null,“name”:“Test Network”},“id”:“501b468c-9dca-4aae-947f-55554c504263”,“networkType”:1,“controllerIp”:”:“654.54.64.654”,“重定向类型”:1,“网络成本”:39,“网络权重”:4,“注释”:“这是一个新的网络注释”,“名称”:“Bills new network ABC”}]
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;
}
});