Tuesday, 27 May 2014

AngularJS executing controller twice

During my angularJS adventure i noticed that my controller seems to be running twice, something like getting two alert when testing for result or double responses during HTTP post, like every nubie to a technology, i had asked myself same question i had asked myself as pass through previous hurdle, i had thought to myself have i made a great mistake in choosing this framework.

So After pulling up my pants and sucking it up, i started googling and found out i had made a mistake but not of the choice of framework but that of my code.When i start my first app with angularjs, i started buy writing :

<div data-ng-controller="MyCrtl">
but then i got to the ease of routing then i switch with :
$routeProvider.when('/',{ templateUrl:'patials/sample.html', controller:'MyCrtl'});
but then i left the previous in my 'sample.html' code which was apparently wrong the meaning of this as i gathered is that i am now instructing AngularJS, to digest my controller twice. 

Solution:

Remove the data-ng-controller="'MyCrtl"' from your html code if you have specified controller:'MyCrtl' I hope this can help someone out there.

1 comment:

  1. Many greetings and tks to the webmasters of this website and Admin for always for sharing reliable information and well researched subject on the angularjs which otherwise can be learned either at regular or only at online Angularjs training centers.

    ReplyDelete