angularjs share data config between controllers

后端 未结 4 961
予麋鹿
予麋鹿 2020-11-28 15:52

I\'m wondering what could be a good way to share directive between controller. I\'ve got ie two directives to use in different controller with different configuration the fi

4条回答
  •  忘掉有多难
    2020-11-28 16:02

    What you want is terrible.

    You wouldn't want your controllers to know anything about each other, let alone, one having access to the function of the other. You can just use a Service to achieve that. As for using directives, not sure what exactly you want to happen.

    As for your second thing, you can as easily do this

    .service('MyTestService', function(){
        return {
           testScope: function(){
               console.log('It works');
           }
        };
    })
    
    .controller('MyController', ['$scope', 'MyTestService', function($scope, MyTestService){
       $scope.testScope = MyTestService.testScope;
    }])
    

    and in your view:

    ppp

提交回复
热议问题