我有两个指令,想要访问hossein
指令中定义的控制器。
指令
app.directive('hossein', function() {
return {
restricted: 'A',
require: '?ngModel',
scope: {
name: '=ngModel'
},
link: function(scope, element, attributes, ngModel) {
console.log(",,,,,,,,,,,,,,,,,,,", scope.name);
},
controller: function($scope) {
var name = $scope.name;
this.fu = function() {
return name;
}
}
}
});
app.directive('shahab', function() {
return {
restricted: 'A',
require: '^?hossein',
scope: {
name: '=ngName'
},
link: function(scope, element, attributes, controll) {
console.log(",,,,,,,,gfddfdfg,,,,,,,,,,,", controll);
scope.name = controll.fu();
}
}
});
标记
<div ng-app="app">
<div ng-controller="maincontroller">
<form ng-controller="newcontroller" name="myform" id="form">
<h2>AngularJS</h2>
<p>Name:
<input name="user" type="text" hossein ng-model="name">
</p>
<p>Family:
<input name="famil" type="text" shahab ng-name="">
</p>
</form>
</div>
</div>
答案 0 :(得分:3)
您必须使用嵌套的 shahab 和 hossein 指令,以便可以访问外部控制器。
<hossein ng-model="name">
<shahab ng-name="">
</shahab>
</hossein>