简体   繁体   English

如何在我的情况下设置我的Angular指令?

[英]How to setup my Angular directive in my case?

I am trying to create a directive with isolate scope. 我正在尝试创建具有隔离范围的指令。

I have 我有

angular.module('myApp').directive('itemCollection',  
['$cookies',     
    function($cookies) {
        return {
            restrict: 'E',
            scope: {
                items: '='
            },
            link: function(scope) {
                    console.log(scope.items)
                    console.log(items)
                    --> both console.log show undefined
            }
        };
    }
]);

Html HTML

<item-collection items="item1"></item-collection>

I can't seem to get the 'item1' in my directive and it's undefined. 我似乎无法在指令中获得'item1' ,并且它是未定义的。 I am not sure what I am missing. 我不确定我缺少什么。 Can anyone help me about it? 有人可以帮我吗? Thanks! 谢谢!

Does item1 come asynchronusly? item1是否异步出现? If it is, you need to set up a watch in your directive and process as soon as it is available. 如果是这样,则需要在指令中设置监视并尽快对其进行处理。

    return {
        restrict: 'E',
        scope: {
            items: '='
        },
        link: function(scope) {
                console.log(scope.items)
                console.log(items)
                --> both console.log show undefined

            scope.$watch(
                function() { return scope.items; },
                function(newValue) {
                    if (newValue != null) {
                        console.log('not null:' + newValue);
                    }
                    else {
                        console.log('still null, skip');
                    }
                }
            );
        }
    };

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

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