【发布时间】:2014-04-27 10:04:35
【问题描述】:
我是 angularjs 的新手,我阅读了一些文献并遵循了很多教程,但我仍然觉得我完全糊涂了。
我当前的问题是自定义指令和隔离范围。我要做的就是将带有@绑定的“字符串”传递给我的使用隔离范围的指令,我不明白我做错了什么。特别是为什么当我使用模板时一切正常,而当template 已经在 DOM 中时,一种数据绑定方式不起作用。
我的代码中的主要部分:
HTML
<div my-directive my-title="TITLE ONE WAY Data Binding">
<div>
<div>This directive is <span style="color:red;">NOT using template</span></div>
<div>
$scope.title = <small><pre>{{title}}</pre></small>
</div>
</div>
</div>
<div my-directive-with-template my-title="TITLE ONE WAY Data Binding"
>
<!-- this directive use a template -->
</div>
JS
var app = angular.module('app', []);
app.directive('myDirective', function() {
return {
restrict: 'AE',
scope:{
title: "@myTitle"
},
link: function(scope, ele, attrs, c) {
console.log('non template directive link:',scope.title,attrs.myTitle);
},
controller:['$scope', function($scope){
console.log('non template directive controller:',$scope.title);
}]
};
});
app.directive('myDirectiveWithTemplate', function() {
return {
restrict: 'AE',
scope:{
title: "@myTitle"
},
link: function(scope, ele, attrs, c) {
console.log('using template directive link:',scope.title,attrs.myTitle);
},
controller:['$scope', function($scope){
console.log('using template directive link:',$scope.title);
}],
template:'<div><div>This directive is using template</div><div>$scope.title = <small><pre>"{{title}}"</pre></small></div></div>',
replace:true
};
});
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope