How do I share $scope data between states in angularjs ui-router?

后端 未结 5 2062
执笔经年
执笔经年 2020-11-22 07:23

Without using a service or constructing watchers in the parent controller, how would one give children states access to the main controller\'s $scope.

5条回答
  •  春和景丽
    2020-11-22 08:05

    The idea is that you use scope in parent->child inheritance:

     .state("main", {
          controller:'mainController',
          abstract: true,
          url:"/main",
          templateUrl: "main_init.html"
      })  
      .state("main.1", {
          controller:'mainController1',
          parent: 'main',
          url:"/1",
          templateUrl: 'form_1.html'
      })  
      .state("main.2", {
          controller:'mainController2',
          parent: 'main',
          url: "/2",
          templateUrl: 'form_2.html'
      })  
    

    Than the usage is simple, you have 3 controllers, one is shared (mainController) and each view has it's own.

提交回复
热议问题