如何将参数从指令传递到指令
how to pass argument from directive to directive
我有两个指令,我想将参数从一个指令传递到另一个指令。
样本:
指令 1:-
app.directive('myDirective1', [function () {
return {
restrict : 'E',
templateUrl : 'templates/myDirective1.html',
link : function (scope, elem, attr) {
scope.items = 'myPara';
}
}]);
指令 2:-
app.directive('myDirective2', [function () {
return {
restrict : 'E',
templateUrl : 'templates/myDirective2.html',
scope : {
items : '='
}
link : function (scope, elem, attr) {
//here i want 'myPara'
}
}]);
Html:-
<my-directive1 items="items">
<my-directive2></my-directive2>
</my-directive1>
在上面的例子中,当我在 Directive1 中更改 scope.items
的值时,它应该反映在 directive2 隔离范围(项目)上。现在我无法获得 Directive2 中的值。谁能帮我。谢谢
在两个指令中都有一个服务..
app.service('myService',function(){ return {myPara : undefined}})
现在将此服务添加到您的两个指令中并像 myService.myPara = bla
一样使用 myPara
因为该服务是单例的,所以您将在两个指令中拥有相同的实例。
指令 1:
app.directive('myDirective1', ['myService',function (myService) {
return {
restrict : 'E',
templateUrl : 'templates/myDirective1.html',
link : function (scope, elem, attr) {
scope.items = myService.myPara;
}
}]);
指令 2
app.directive('myDirective2', ['myService',function (myService) {
return {
restrict : 'E',
templateUrl : 'templates/myDirective2.html',
scope : {
items : '='
}
link : function (scope, elem, attr) {
//here i want 'myPara'
myService.myPara // Here is your myPara
}
}]);
我有两个指令,我想将参数从一个指令传递到另一个指令。
样本:
指令 1:-
app.directive('myDirective1', [function () {
return {
restrict : 'E',
templateUrl : 'templates/myDirective1.html',
link : function (scope, elem, attr) {
scope.items = 'myPara';
}
}]);
指令 2:-
app.directive('myDirective2', [function () {
return {
restrict : 'E',
templateUrl : 'templates/myDirective2.html',
scope : {
items : '='
}
link : function (scope, elem, attr) {
//here i want 'myPara'
}
}]);
Html:-
<my-directive1 items="items">
<my-directive2></my-directive2>
</my-directive1>
在上面的例子中,当我在 Directive1 中更改 scope.items
的值时,它应该反映在 directive2 隔离范围(项目)上。现在我无法获得 Directive2 中的值。谁能帮我。谢谢
在两个指令中都有一个服务..
app.service('myService',function(){ return {myPara : undefined}})
现在将此服务添加到您的两个指令中并像 myService.myPara = bla
一样使用 myPara
因为该服务是单例的,所以您将在两个指令中拥有相同的实例。
指令 1:
app.directive('myDirective1', ['myService',function (myService) {
return {
restrict : 'E',
templateUrl : 'templates/myDirective1.html',
link : function (scope, elem, attr) {
scope.items = myService.myPara;
}
}]);
指令 2
app.directive('myDirective2', ['myService',function (myService) {
return {
restrict : 'E',
templateUrl : 'templates/myDirective2.html',
scope : {
items : '='
}
link : function (scope, elem, attr) {
//here i want 'myPara'
myService.myPara // Here is your myPara
}
}]);