AngularJS reinitialize controller

可紊 提交于 2019-12-05 09:42:08
Pankaj Parkar

You need to tell state that reload controller each time when URL is getting accessed via browser by just adding adding reload option of state to true like reload: true.

Code

.state('new_group', {
    url: '/new_group',
    templateUrl: 'templates/new_group.html',
    controller: 'newGroupCtrl',
    reload: true //will reload controller when state is being access
});

You should use $state.go('new_group') instead of doing $window.location.href = ('#/new_group'); which will ensure that the route changes will recognize by ui-router.

Same SO answer here

Since you are using Ionic Framework (Good Job), you can do this:

.controller('YourCtrl', function($ionicView){
  $ionicView.enter(function(){
    //code that you want to run, each time the view is active
  });
});

Remove the controller from :

.state('new_group', {
    url: '/new_group',
    templateUrl: 'templates/new_group.html',
})

And add the controller at "new_group.html" page with parent tag of the page like:

<div ng-controller="newGroupCtrl"></div>
Lukáš Černý

Also I found helpful (for Ionic Framework) to use

.state('new_group', {
    url: '/new_group',
    templateUrl: 'templates/new_group.html',
    cache: false
})

Reference to similar question and problems: Reloading current state - refresh data or in sjm's answer in Reinitialize Controller every time when visiting View - Ionic Framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!