我有一个像这样的部分
<my-partial attr1="some text" attr2="some other text">
<div ng-show="displayBool">
<div>{{attr1}}</div>
<div>{{attr2}}</div>
<button ng-click="changeBool()">Change var</button>
</my-partial>
呈现
<div>some text</div>
<div>some other text</div>
在控制器中我设置了$scope.displayBool = true
。指令是这样的:
angular.module('grasshopperApp')
.directive('myPartial', function () {
return {
restrict: 'EA',
scope: {
displayBool: '=',
attr1: '@'
attr2: '@'
},
templateUrl: '../my.html'
};
});
displayBool
在指令中没有出现,div从未显示,但是当我在开发人员面板中检查隐藏元素时,属性值显示正确。为什么会这样?我怎样才能让这个价值体现出来?
你可以指定一个控制器到你的指令,并设置一些属性。
所以你可以这样做:
指令
(function(){
function directive() {
return {
restrict: 'EA',
scope: {
attr1: '@',
attr2: '@'
},
templateUrl: 'template.html',
//Declare controller for our custom directive
controller: function($scope){
//Set displayBool property
$scope.displayBool = true;
}
};
}
angular
.module('app')
.directive('myPartial', directive);
})();
模板
<div ng-show="displayBool">
<div>{{attr1}}</div>
<div>{{attr2}}</div>
</div>