自定义指令范围元素可见但未定义

时间:2016-09-28 13:38:46

标签: angularjs scope angular-directive

我制作了一个自定义指令,用于从控制器中检索数据。

我的变量在范围元素中可见,但在尝试访问它时,我得到 undefined

HTML

<map borders="vm.borders"></map>

指令

angular.module('myApp.directives')
.directive('map', [ function() {
    return {
        restrict: 'E',
        scope: {
            borders: '='
        },
        link: function(scope, element, attrs) {
            console.log(scope); //cfr linked image
            console.log(scope.borders) //undefined
        }
    }
}]);

这是范围。它包含border变量。

scope console

我在检索此边框值时缺少什么?

1 个答案:

答案 0 :(得分:3)

我可以建议在指令中添加ng-if,因为例如,如果vm.borders来自承诺,则需要ng-if

<map borders="vm.borders" ng-if="vm.borders"></map>
相关问题