我创建了两个指令并将第一个指令插入到第二个指令中。模板属性的内容工作正常,但无法识别控制器的范围变量。请提供我的解决方案
答案 0 :(得分:1)
您未提供第二个指令的属性。
<强> HTML 强>
<div second-dir first-dir-scope="content">
<div first-dir first-dir-scope="content"></div>
</div>
答案 1 :(得分:1)
最好的选择是使用父指令,我们可以在require
require: '?secondDir'
指令,例如firstDir
<强>代码强>
var myApp = angular.module("myApp", []);
myApp.controller("myController", function($scope) {
$scope.content = "test1";
});
myApp.directive("firstDir", function() {
return {
restrict: "AE",
require: '?secondDir',
scope: {
firstDirScope: "="
},
template: "<div>first content</div>",
link: function(scope, element, attrs, secondDir) {
console.log(scope.firstDirScope, 'first');
}
};
});
myApp.directive("secondDir", function() {
return {
restrict: "AE",
scope: {
firstDirScope: "="
},
controller: function($scope) {
console.log($scope.firstDirScope, 'second');
}
};
});