为什么指令控制器中定义的变量不起作用?
Why variable defined in the directive controller not working?
为什么我在controller中定义了'sayHi',在模板中却没有显示?
js:
(function(){
var app = angular.module('myApp', []);
app.directive("directive1", function(){
return {
restrict : 'E',
scope: {
},
link : function($scope){
},
controller: ['$scope', function($scope){
$scope.sayHi = 'hi';
window.console.log($scope.sayHi);
}]
};
});
})();
html:
<div style="border: 1px solid; padding: 10px; min-height: 100px;">
Directive1 :
<directive1>
{{sayHi}}
</directive1>
</div>
您的指令没有任何模板。您需要添加
template: '{{ sayHi }}',
到你的指令定义。 <directive1></directive>
里面的内容不构成指令的模板。
为什么我在controller中定义了'sayHi',在模板中却没有显示?
js:
(function(){
var app = angular.module('myApp', []);
app.directive("directive1", function(){
return {
restrict : 'E',
scope: {
},
link : function($scope){
},
controller: ['$scope', function($scope){
$scope.sayHi = 'hi';
window.console.log($scope.sayHi);
}]
};
});
})();
html:
<div style="border: 1px solid; padding: 10px; min-height: 100px;">
Directive1 :
<directive1>
{{sayHi}}
</directive1>
</div>
您的指令没有任何模板。您需要添加
template: '{{ sayHi }}',
到你的指令定义。 <directive1></directive>
里面的内容不构成指令的模板。