I understand AngularJS runs through some code twice, sometimes even more, like $watch
events, constantly checking model states etc.
However my code:
function MyController($scope, User, local) {
var $scope.User = local.get(); // 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 into my DB. I'm clearly still learning as I've been banging my head against this for ages!
Best Answer
The app router specified navigation to
MyController
like so:But I also had this in
home.html
:This digested the controller twice. Removing the
data-ng-controller
attribute from the HTML resolved the issue. Alternatively, thecontroller:
property could have been removed from the routing directive.This problem also appears when using tabbed navigation. For example,
app.js
might contain:The corresponding reports tab HTML might resemble:
This will also result in running the controller twice.