AngularJS: How can I pass variables between controllers?

后端 未结 16 2784
误落风尘
误落风尘 2020-11-22 00:31

I have two Angular controllers:

function Ctrl1($scope) {
    $scope.prop1 = \"First\";
}

function Ctrl2($scope) {
    $scope.prop2 = \"Second\";
    $scope.         


        
16条回答
  •  耶瑟儿~
    2020-11-22 01:27

    Besides $rootScope and services, there is a clean and easy alternative solution to extend angular to add the shared data:

    in the controllers:

    angular.sharedProperties = angular.sharedProperties 
        || angular.extend(the-properties-objects);
    

    This properties belong to 'angular' object, separated from the scopes, and can be shared in scopes and services.

    1 benefit of it that you don't have to inject the object: they are accessible anywhere immediately after your defination!

提交回复
热议问题