我有一个定义如下的指令:
angular.module("main.directives").directive("todo", function() {
return {
restrict: "A",
scope: {
todo: "=entity"
},
replace: false,
templateUrl: "todo.html",
link: function(scope, element, attributes) {
}
};
});
我使用的模板如下:
<div todo entity="todoData"></div>
todoData
来自控制器或本地作用域的其他部分。总之,这一切都像一个魅力,所以这是很酷的!
我的问题是:如何修改指令定义,以便它也能与这种类型的标记一起工作:
<div todo="todoData"></div>
如您所见,数据现在作为标记指令的属性的值传入。就像ng-
指令一样:
<p ng-repeat="bit in data"></p>
<p ng-click="whatever()"></p>
如何才能做到这一点?
谢谢
发布于 2015-03-03 13:27:38
替换
scope: {
todo: "=entity"
},
通过
scope: {
todo: "=todo"
},
或者简单的
scope: {
todo: "="
},
发布于 2017-11-02 02:26:39
当您用angularjs编写属性指令时,您可能希望它得到一个属性值。例如,如下所示:
<div my-attribute="somevalue"></div>
那么,您如何创建一个新的范围来接受它呢?这并不明显。这就是你怎么做的
app.directive('myAttribute', function() {
return {
restrict: 'A',
scope: {
myAttribute: '='
},
template: '<div style="font-weight:bold">{{ myAttribute | number:2}}</div>'
};
});
注意到的诀窍是,"self属性“是因为在camel情况下属性的名称。
这是参考这个答案!
发布于 2015-03-03 13:35:00
您必须在inself中评估属性的值。隔离作用域不是我最喜欢的指令范围之一。相反,您可以使用scope = true从父控制器继承。这将允许您使用在父作用域上公开的所有变量。
在你的案子里。
angular.module("main.directives").directive("todo", function() {
return {
restrict: "A",
scope: true,
replace: false,
templateUrl: "todo.html",
link: function(scope, element, attributes) {
scope.todo = scope.$eval(attributes[todo]);
}
};
});
现在可以使用你的todo指令了。就像其他的指令一样。
示例:
<div todo="getTodoList()"></div>
<div todo="[{description:'hahahha'}]"></div>
https://stackoverflow.com/questions/28842145
复制相似问题