Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在两个指令中绑定共享服务_Angularjs_Angularjs Scope - Fatal编程技术网

Angularjs 在两个指令中绑定共享服务

Angularjs 在两个指令中绑定共享服务,angularjs,angularjs-scope,Angularjs,Angularjs Scope,我想要完成的是绑定两个抛出共享服务的指令,这样当其中一个发生更改时,更改将立即传播到另一个 我的指令如下: app.directive("input1", function (sharedService) { return { restict: 'A', scope: 'isolate', template: '<input type="text" ng-model="sharedText" class="input-medium"

我想要完成的是绑定两个抛出共享服务的指令,这样当其中一个发生更改时,更改将立即传播到另一个

我的指令如下:

app.directive("input1", function (sharedService) {
    return  {
        restict: 'A',
        scope: 'isolate',
        template: '<input type="text" ng-model="sharedText" class="input-medium" />{{sharedText}}',
        link: function (scope, elem, attrs) {
            scope.sharedText = sharedService.sharedText; // Service never gets updated if the scope it's modified :(
        }
    }
});

app.directive("input2", function (sharedService) {
    return  {
        restict: 'A',
        scope: 'isolate',
        template: '<input type="text" ng-model="sharedText" class="input-medium" />{{sharedText}}',
        link: function (scope, elem, attrs) {
            scope.sharedText = sharedService.sharedText;
        }
    }
});
app.directive(“input1”,函数(sharedService){
返回{
restict:“A”,
范围:'隔离',
模板:“{sharedText}}”,
链接:功能(范围、要素、属性){
scope.sharedText=sharedService.sharedText;//如果修改了服务的作用域,则不会更新服务:(
}
}
});
应用程序指令(“输入2”,功能(共享服务){
返回{
restict:“A”,
范围:'隔离',
模板:“{sharedText}}”,
链接:功能(范围、要素、属性){
scope.sharedText=sharedService.sharedText;
}
}
});
在这里,你可以看到我到目前为止所拥有的东西:
http://jsfiddle.net/Hubrus/kxGG6/1/


谢谢!

由于代码的原型性质,您需要使用
符号或对象。直接字符串绑定在当前作用域上创建新的字符串值

看到这把小提琴了吗