【发布时间】:2014-03-11 11:37:18
【问题描述】:
app.directive('appView', ['appService',function($scope) {
return {
restrict : '',
template : '',
templateUrl : 'app/app.html',
controller : function($scope,docsService){
},
compile : function(tElement, tAttrs, transclude){
tElement.bind('click', function() {
console.log("In compile");
//Update DOM element
});
},
//or
/*
link : function(scope, element, attrs) {
element.bind('click', function() {
console.log("In link");
//Update DOM element.
});
},*/
};
}]);
我正在探索 AngularJS,发现编译和链接不能一起工作,
现在,要操作 DOM 并监听事件,这里的最佳实践是使用链接还是编译?为什么选择一个而不是另一个?链接有一个范围属性,我猜可以更灵活。
【问题讨论】:
-
取决于您的需求。 have a read at the docs.
标签: javascript angularjs