Combating AngularJS executing controller twice -
i understand angularjs runs through code twice, more, $watch
events, checking model states etc.
however code:
function mycontroller($scope, user, local) { var $scope.user = local.get(); // locally save user data user.get({ id: $scope.user._id.$oid }, function(user) { $scope.user = new user(user); local.save($scope.user); }); //...
is executed twice, inserting 2 records db. i'm still learning i've been banging head against ages!
the app router specified navigation mycontroller
so:
$routeprovider.when('/', { templateurl: 'pages/home.html', controller: mycontroller });
but had in home.html
:
<div data-ng-controller="mycontroller">
this digested controller twice. removing data-ng-controller
attribute html resolved issue. alternatively, controller:
property have been removed routing directive.
this problem appears when using tabbed navigation. example, app.js
might contain:
.state('tab.reports', { url: '/reports', views: { 'tab-reports': { templateurl: 'templates/tab-reports.html', controller: 'reportsctrl' } } })
the corresponding reports tab html might resemble:
<ion-view view-title="reports"> <ion-content ng-controller="reportsctrl">
this result in running controller twice.
Comments
Post a Comment