如何在指令定义对象的模板中使用指令中声明的变量?
How do you use a variable declared in a directive in a Directive Definition Object's template?
这是我的简单指令:
myApp.directive('myDirective', function() {
var someVar = "Yodelayheehoo";
return {
restrict: 'E',
template: "<p>The var is: {{someVar}}</p>"
}
});
我试过了,但是 someVar
没有打印出来。
我错过了什么?
someVar
必须是范围对象的 属性 而不仅仅是局部变量:
myApp.directive('myDirective', function() {
return {
restrict: 'E',
template: "<p>The var is: {{someVar}}</p>",
link: function(scope) {
scope.someVar = "Yodelayheehoo";
}
}
});
这是我的简单指令:
myApp.directive('myDirective', function() {
var someVar = "Yodelayheehoo";
return {
restrict: 'E',
template: "<p>The var is: {{someVar}}</p>"
}
});
我试过了,但是 someVar
没有打印出来。
我错过了什么?
someVar
必须是范围对象的 属性 而不仅仅是局部变量:
myApp.directive('myDirective', function() {
return {
restrict: 'E',
template: "<p>The var is: {{someVar}}</p>",
link: function(scope) {
scope.someVar = "Yodelayheehoo";
}
}
});