UI-Router Multiple Views Single Controller not work

前端 未结 1 1780
故里飘歌
故里飘歌 2020-12-06 07:58

I would like to use one controller defined in views, but the $scope does not define anything. Is there a way to do this? Please share a simple example in order

相关标签:
1条回答
  • 2020-12-06 08:37

    Very similar issue: Why controller does not work in UI-router of angularjs?

    The point here is:

    Controller always belongs to View, never to state.

    Other words, to use same type of controller (but two instances for each view), we have to do that kind of declaration:

     $stateProvider.state('Home', {
        url: '/',
    
        // instead of this
        //controller: "HomeCtrl",
    
        views: {
          "a": {
            templateUrl: 'templates/a.html',
            controller: "HomeCtrl", // we need this
          },
          "b": {
            templateUrl: 'templates/b.html',
            controller: "HomeCtrl", // and also this
          }
        }
      });
    

    In case, we want to share some stuff among many views, we need different technique than "same controller". See:

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

    Another insight, could be covered here:

    scope and controller instantiation with ui router

    And including typescript, there is a detailed description and example how all views/states could target some common RootModel

    Angular Digest cycle being ran but ng-bind value not updating

    0 讨论(0)
提交回复
热议问题