我制作了一个自定义指令,用于从控制器中检索数据。
我的变量在范围元素中可见,但在尝试访问它时,我得到 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变量。
我在检索此边框值时缺少什么?
答案 0 :(得分:3)
我可以建议在指令中添加ng-if
,因为例如,如果vm.borders
来自承诺,则需要ng-if
:
<map borders="vm.borders" ng-if="vm.borders"></map>