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

后端 未结 3 1879
独厮守ぢ
独厮守ぢ 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:46

    JavaScript passes objects by reference, so all scopes will point to the same object. Why not just do this?

    app.factory("sharedData", function() {
        return {data: "init text from factory"};
    });
    
    app.controller("first", function($scope, sharedData) {
        $scope.sharedData = sharedData;
    });
    
    app.controller("second", function($scope, sharedData) {
        $scope.sharedData = sharedData;
    });
    

    and in your view:

    {{sharedData.data}}

提交回复
热议问题