AngularJS : How to create a two-way data binding between two isolated controllers and a shared service?

后端 未结 3 1859
独厮守ぢ
独厮守ぢ 2020-12-31 08:29

I am trying to create a two-way data binding between two isolated controllers and a shared service (which provides another isolated scope):

app.factory(\"sha         


        
3条回答
  •  一生所求
    2020-12-31 08:50

    Fixed it. References will be lost if you are using primitives, as in your fiddle.

    Check this:

    Updated fiddle

    app.factory("sharedScope", function($rootScope) {
        var scope = $rootScope.$new(true);
        scope.data = {text: "init text from factory"};
        return scope;
    });
    
    app.controller("first", function($scope, sharedScope) {
        $scope.data1 = sharedScope.data;
    });
    
    app.controller("second", function($scope, sharedScope) {
        $scope.data2 = sharedScope.data;
    });
    

提交回复
热议问题