Use multiple controllers in the same view with ionic

I have a view with the controller name "Listctrl"
in this view i want to have a different controller named "LocationCtrl"
. At the moment i do this like this:

Routing

.state('list', {
        url: '/list',
        templateUrl: 'templates/list.html',
        controller: "ListCtrl",
        cache: false
      })

HTML (list.html)

  
    

STORIE

How should i solve this correctly? I need two controllers for the same view, but on different places, because i want to reuse the controller code in different views.

At the moment
does not run the LocationCtrl

Any help much appreciated!

There is no possibility to have two controllers for one view, as this doesn’t make sense. If you have a functionality that should be shared, use controller inheritance, but this is possible only if LocationCtrl
adds its methods to $scope
:

var app = angular.module('angularjs', []);

app.controller('LocationCtrl', function($scope) {
  // I have functionality to share
});

app.controller('ListCtrl', function($scope, $controller) {
  $controller('LocationCtrl', {$scope: $scope}); // This adds properties to ListCtrl's scope
});

Another approach could be to put ng-controller="LocationCtrl"
to a wrapper div:

  

STORIE

But this doesn’t seem like a good option. A better approach would be to create a component/directive with the functionality defined on LocationCtrl
and use it somewhere in your view:

  
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Use multiple controllers in the same view with ionic

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录