angularjs - Controller executing twice with ui-router nested states (Ionic) -
problem
my ionic app lets choose project side menu, , displays 2 tabs (tasks, messages) in main content area. tasks , messages tabs nested states of project.
when change projects in side menu, tasklistctrl
gets executed twice. see live demo , watch console change between projects. have video shows issue in detail.
how stop tasklistctrl
executing twice? there better way structuring these nested states?
code
full code on github »
here's $stateprovider
config:
.state('project', { url: "/projects/:projectid", abstract: true, cache: false, controller: 'projectdetailctrl', templateurl: "templates/project.tabs.html", resolve: { project: function($stateparams, projects) { return projects.get($stateparams.projectid); } } }) .state('project.tasks', { url: '/tasks', views: { 'tasks-tab': { templateurl: 'templates/task.list.html', controller: 'tasklistctrl' } } })
and relevant snippet controllers.js
:
.controller('projectdetailctrl', function($scope, project) { $scope.project = project; console.log('=> projectdetailctrl (' + $scope.project.name + ')') }) .controller('tasklistctrl', function($scope, $stateparams) { $scope.tasks = $scope.project.tasks; console.log('\t=> tasklistctrl') console.log('\t\t=> $stateparams: ', $stateparams) console.log('\t\t=> $scope.tasks[0].title: ', $scope.tasks[0].title) })
resources
- live demo (watch console logs change between projects)
- video showing issue
- code on github
notes
- i aware there similar questions on stackoverflow — however, none of solutions offer solved issue.*
- i've read can happen when attaching controller both in
$stateprovider
,ng-controller
— however, i've checked , i'm not doing this. i'm attaching controller$stateprovider
.
i guess tuckerjt07 right.
it seems issue routing , parameters , ionic tabs. i've spend whole day trying figure out going on.
i thought problem fact you're using abstract controller parameters, that's not problem.
i've checked if side menu interfering tabs but, again, problem not there.
i've checked scope trying eliminate friction using controlleras
, avoiding reference $scope object store viewmodel ... nothing.
i've created simplified version of application here.
there's not in there , navigation through constants in header. can see problem still there.
doing little bit of debugging seems problem sits here.
line calls controller twice. can check adding breakpoint @ line 48435 in ionic.bundle.js.
the option have change project.tabs.html , load list of tasks without sub-view. this:
<ion-view view-title="{{ project.name }}: tasks"> <ion-tabs class="tabs-icon-top tabs-positive"> <ion-tab title="{{ project.name }} tasks" icon="ion-home"> <ion-nav-view> <ion-content> <ion-list> <ion-item class="item-icon-right" ng-repeat='task in project.tasks'> {{ task.title }} <i class="icon ion-chevron-right icon-accessory"></i> </ion-item> </ion-list> </ion-content> </ion-nav-view> </ion-tab> <ion-tab title="about" icon="ion-ios-football" ui-sref="tabs.tab2"> <ion-nav-view name="tabs-tab2"></ion-nav-view> </ion-tab> <ion-tab title="another" icon="ion-help-buoy" ui-sref="tabs.tab3"> <ion-nav-view name="tabs-tab3"></ion-nav-view> </ion-tab> </ion-tabs> </ion-view>
you can check how works here.
i guess should open issue.
Comments
Post a Comment