AngularJS: Inject controller inside another controller from the same module

前端 未结 4 997
耶瑟儿~
耶瑟儿~ 2021-02-12 16:12

Is possible to inject a controller into another controller that is part of the same module?

example:

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-12 16:38

    You need to use $controller dependency by using that you can inject one controller to another

    .controller('controllerTwo', ['$scope', '$controller', function($scope, $controller){
      $controller('controllerOne', {$scope: $scope})
      //inside scope you the controllerOne scope will available
    }]);
    

    But do prefer service/factory to share data

提交回复
热议问题