javascript - 如何使用 underscore.js 将 Angular 变量从 ng-repeat 注入(inject)到过滤器中

标签 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">

这就是“site.networkId”的值,这是我获取网络名称所必须的。

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

名称位于另一个 JSON 数组中...所以我尝试将此值传递到 ANGULAR 的 FILTER 中,如下所示:

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

其中 name 是我需要的最终值,netNames 是过滤器,site.networkId 是我在上述 ng-repeat 中的参数。

这是我的过滤器:

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

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

        return networkName;
});

因此,如果我有 Handlebars ,我只需创建一个助手并“噗”地一声,就会呈现所需的结果。有 Angular ;没那么容易,或者也许我没有看到它。

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

简单;对吗?

更新:这是我的 Controller ...

// 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;
    }

最佳答案

您没有以正确的方式将 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 中。

关于javascript - 如何使用 underscore.js 将 Angular 变量从 ng-repeat 注入(inject)到过滤器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21652378/

相关文章:

javascript - 如何在 JavaScript 中动态创建无序列表?

javascript - 谷歌分析答案属性有什么用?

javascript - 如何使用 Google Tag Manager Click Event 获取隐藏输入的属性

javascript - underscore js - 查找嵌套对象

javascript - 为什么同一个 HSM key 每次都会验证到不同的以太坊地址?

javascript - NodeJS : Run dot slash command line Windows

javascript - 在页面模板中使用 Angular ui-router State

javascript - 如何使用 ui-grid 选择单行?

jquery - 如何在一个文件中存储多个html模板?

javascript - 下划线,过滤掉不需要的对象