json - AngularJS calls HTTP multiple times in controller -
i developing page angular, , have init() method in controller. code follows:
var filterscontroller = ['$scope', '$http', function ($scope, $http) { $scope.init = function () { $http({ method: 'get', url: '/json-tags-test', cache: true }).success(function (data, status, headers, config) { // callback called asynchronously // when response available }).error(function (data, status, headers, config) { // called asynchronously if error occurs // or server returns response error status. }); }; }];
it call simple json file.
my html follows:
<div class="container main-frame" ng-app="projectsapp" ng-controller="filterscontroller" ng-init="init()"> </div>
for reason, call gets call twice every time load page. standard behaviour?
many thanks,
dash
this problem may caused having ng-app
routing controller , ng-controller
reference in page. example, if app looks like:
<html lang="en" ng-app="myapp"> <head>...</head> <body> <div ng-controller="mycontroller"> ... </div> </body> </html>
javascript defining app:
angular.module('myapp',[]) { $routeprovider.when('/path', {templateurl: '...', controller: mycontroller);
in case above, defined routing mycontroller, controller instantiated twice , you'll see 2 calls described.
updated
above code describe problem proper solution missing updated answer per @intrepid comment.
need remove ng-controller="mycontroller"
html template if defined in route.
Comments
Post a Comment