简体   繁体   English

将变量传递给指令AngularJS

[英]pass variable to directive AngularJS

I am passing an array to the directive which is used for an ng-repeat. 我将数组传递给用于ng-repeat的指令。 I also would like to pass a variable which is bound to an input field. 我还想传递一个绑定到输入字段的变量。 The way i am doing it, isn't showing a thing. 我这样做的方式,没有表现出任何东西。

Here is the HTML code: (The part where I call my directive is on the bottom) 这是HTML代码:(我调用指令的部分在底部)

<div class="widget-body" id="widget-dispatch">
                <div class="padding-dispatch">
                    <form id="dispatch-search">
                        <input id="search-fld" type="text" name="param" placeholder="To search type here..." ng-model="searchDispatch">
                    </form>
                    <div class="category-list">
                        <a class="category-link active" ng-click="dispatchList(dataPersons)">
                            <span>Persons</span><span class="category-badge active">99</span>
                        </a>
                        <span class="vertical-devider"> </span>
                        <a class="category-link" ng-click="dispatchList(dataProjects)">
                            <span>Projects</span><span class="category-badge">15</span>
                        </a>
                        <span class="vertical-devider"> </span>
                        <a class="category-link" ng-click="dispatchList(dataConstructionYards)">
                            <span>Construction yards</span><span class="category-badge">32</span>
                        </a>
                        <span class="vertical-devider"> </span>
                        <a class="category-link" ng-click="dispatchList(dataContainers)">
                            <span>Containers</span><span class="category-badge">85</span>
                        </a>
                        <span class="vertical-devider"> </span>
                        <a class="category-link" ng-click="dispatchList(dataVehicles)">
                            <span>Vehicles</span><span class="category-badge">66</span>
                        </a>
                    </div>
                </div>
                <div id="dispatch-list">
                    <div class="dispatch-categories first"><strong>Dispatched to</strong></div>
                    <div class="dispatch-categories"><strong>Collective</strong></div>
                    <div class="dispatch-categories"><strong>Unique</strong></div>
                    <div class="dispatch-categories"><strong>Inspections</strong></div>
                    <div class="dispatch-categories"><strong>Unused</strong></div>
                </div>
                <dispatch data='listToDispatch' search='{{searchDispatch}}'></dispatch>
            </div>

Here is the directive: 这是指令:

    define(['dashboard/module', 'lodash'], function (module) {

    'use strict';

    return module.registerDirective('dispatch', function () {
        return {
            controller: 'DashboardCtrl',
            restrict: 'E',
            scope: {
                data: '=',
                search: '@'
            },
            template: '<div class="padding-dispatch" ng-repeat="(name,user) in data | filter:search">' +
                            '<strong>{{name}}</strong>' +
                            '<p>{{search}}</p>' +
                            '<div class="dispatch-charts" chartjsdoughnut="user[0]"></div>' +                            
                            '<div class="dispatch-charts" chartjsdoughnut="user[1]"></div>' +                            
                            '<div class="dispatch-charts" chartjsdoughnut="user[2]"></div>' +                             
                            '<div class="dispatch-charts" chartjsdoughnut="user[3]"></div>' +
                            '<hr />' +
                        '</div>'
        }
    });
});

So, my data is being passed to the directive but the search doesn't. 因此,我的数据将传递给指令,但搜索不会。

Here is the data which listToDispatch contains: 这是listToDispatch包含的数据:

$scope.dataVehicles =
    {
        "Xavier":
        [

            [
                {
                    value: 70,
                    color: "#1675a9",
                    highlight: "#1675a9",
                    label: "is in use"
                },
                {
                    value: 36,
                    color: "#7eb3cf",
                    highlight: "#1675a9",
                    label: "is used"
                }
            ],

            [
                {
                    value: 40,
                    color: "#1675a9",
                    highlight: "#1675a9",
                    label: "is unique"
                },
                {
                    value: 30,
                    color: "#7eb3cf",
                    highlight: "#1675a9",
                    label: "is unique"
                }
            ],

            [
                {
                    value: 70,
                    color: "#1675a9",
                    highlight: "#1675a9",
                    label: "is in use"
                },
                {
                    value: 30,
                    color: "#7eb3cf",
                    highlight: "#1675a9",
                    label: "is used"
                }
            ],

            [
                {
                    value: 70,
                    color: "#1675a9",
                    highlight: "#1675a9",
                    label: "is in use"
                },
                {
                    value: 30,
                    color: "#7eb3cf",
                    highlight: "#1675a9",
                    label: "is used"
                }
            ]
        ],
        "Tarek":
        [

            [
                {
                    value: 70,
                    color: "#1675a9",
                    highlight: "#1675a9",
                    label: "is in use"
                },
                {
                    value: 30,
                    color: "#7eb3cf",
                    highlight: "#1675a9",
                    label: "is used"
                }
            ],

            [
                {
                    value: 76,
                    color: "#1675a9",
                    highlight: "#1675a9",
                    label: "is in use"
                },
                {
                    value: 30,
                    color: "#7eb3cf",
                    highlight: "#1675a9",
                    label: "is used"
                }
            ],

            [
                {
                    value: 70,
                    color: "#1675a9",
                    highlight: "#1675a9",
                    label: "is in use"
                },
                {
                    value: 30,
                    color: "#7eb3cf",
                    highlight: "#1675a9",
                    label: "is used"
                }
            ],

            [
                {
                    value: 70,
                    color: "#1675a9",
                    highlight: "#1675a9",
                    label: "is in use"
                },
                {
                    value: 30,
                    color: "#7eb3cf",
                    highlight: "#1675a9",
                    label: "is used"
                }
            ]
        ]
    };

Looks like the problem is when you are using the search inside the directive: 看起来问题出在指令内部使用search

filter:search

Here, search is a value and needs to be interpolated. 在这里, search是一个值,需要进行插值。

It would probably be better to use search: '=' in the directive isolated scope: 在指令隔离范围内使用search: '='可能会更好:

scope: {
    data: '=',
    search: '='
}

And then change the directive attribute to not interpolate: 然后将指令属性更改为不插值:

<dispatch data='listToDispatch' search='searchDispatch'></dispatch>

If you take a look at the documentation for filter , you'll see that you can only use |filter:search for searching in String arrays. 如果查看filter的文档,您会发现只能使用|filter:search来搜索字符串数组。

I think what you're looking for is 我想你要找的是

|filter:{$:search}

which will search in all of the properties of the Objects contained in listToDispatch. 它将搜索listToDispatch中包含的Objects的所有属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM