以下是我的index.html
<body ng-controller="MainCtrl">
<search myname="myval" change-client='client()'></search>
<pagination></pagination>
</body>
</html>
和app.js
var app = angular.module('plunker', []);
app.directive('search', function($rootScope) {
return {
scope:
{
myname:'=',
changeClient : '&'
},
restrict: 'AE',
replace: true,
template: '<div><input type ="text" ng-model = "bankname">{{myname}}</div>',
link: function(scope, elm, attrs) {
scope.$watch('bankname',function(newVal){
//alert("inside search"+scope.bankname);
$rootScope.myname = scope.bankname;
scope.changeClient();
});
}
};
});
app.directive('pagination', function($rootScope) {
return {
restrict: 'AE',
replace: true,
template: '<div><h1>from pagination</h1></div>',
link: function(scope, elm, attrs) {
// alert("inside pagination"+$rootScope.myname);
scope.$watch('bankname',function(newVal){
alert("inside pagination"+scope.bankname);
});
}
};
});
app.controller('MainCtrl', function($scope,$rootScope) {
$scope.client = function() {
$scope.myval = $rootScope.myname;
// alert("in controller"+$scope.myval);
}
});
下面是plunker的链接
http://plnkr.co/edit/RdKOKMV5Cf6yTXx5PZMt?p=preview
而我想要得到的是当我在文本框中输入一些值时我在搜索指令中获得该值而不是在分页指令链接函数中如何获取分页指令链接函数中的值请建议我该怎么做此
答案 0 :(得分:0)
您可以使用MainCtrl的范围。指令的范围是MainCtrl的子节点。
试试这个:
app.directive('search', function($rootScope) {
return {
restrict: 'AE',
replace: true,
template: '<div><input type ="text" ng-model = "bankname">{{myname}}</div>',
};
});
app.directive('pagination', function($rootScope) {
return {
restrict: 'AE',
replace: true,
template: '<div><h1>from pagination</h1></div>',
link: function(scope, elm, attrs) {
scope.$watch('bankname',function(oldVal, newVal){
if(oldVal !== newVal) {
alert("inside pagination : "+scope.bankname);
}
});
}
};
});
希望这有帮助。