1

I have the code shown below in a controller for an AngularJS Single Page Application. When this page runs and I check Developer Tools Network (in IE, Chrome, and Firefox) it always shows 2 successful GET calls to Tables, and two empty OPTIONS calls to Tables. Why are there 2 GET calls? Is that normal or did I do something wrong in my code? Also, why does it issue the 2 OPTIONS calls?

"use strict";

app.controller('AdminController', function ($scope, $http)
{
    $scope.$parent.Title = "Admin";

    var url = $scope.$parent.BaseUrl + "Tables";
    $http.get(url)
        .then(function mySuccess(response)
        {
            $scope.MyTables = response.data;
        });
});

5
  • 2
    You may have defined your controller more than once... Commented Jun 10, 2016 at 14:47
  • may be you use "resolve" in navigation, the best way to find the issue is to set "debugger" before you call $http service Commented Jun 10, 2016 at 14:49
  • The OPTIONS requests are probably caused by CORS, i.e. you'res sending requests to a host/port other than the one where the scripts come from. Commented Jun 10, 2016 at 14:52
  • JB, I am calling EnableCors. Does that account for the OPTIONS call? Commented Jun 10, 2016 at 14:53
  • M.Doye, I didn't intend to define the controller more than once. In the html page I have <div ng-controller="AdminController">. I thought I need to name it in the html page and then declare it again in the controller. Is that wrong? Commented Jun 10, 2016 at 14:54

1 Answer 1

5

This has happened to me in the past, the issue was that I had defined my controller twice, once in the route:

.state('app.state', {
  url: '/state',
  controller: 'SomeCtrl',
  templateUrl: 'views/state.html'
})

and then defined it again in my view HTML:

<div ng-controller="SomeCtrl"></div>

As far as I am aware, you should only define it in one or the other.

Sign up to request clarification or add additional context in comments.

5 Comments

As noted above in the "controller" page I have app.controller('AdminController', function ($scope, $http) and in the html page I have <div ng-controller="AdminController">. Should I remove the ng-controller in the html page? I thought it needs both to work?
If you have defined it in your route, you don't need to add it to the HTML again
I removed the ng-controller in the html page and I tried it. It works with only one network GET call (and one OPTIONS call). So I have marked your answer as the solution. Now I am confused about how AngularJS defines its controllers. I thought it needed to be marked in the html page so that it would know what part of the page is controlled by that controller.
You can add it to your HTML if you prefer, and remove it from the route, it really depends on your app structure, but here is a pretty simple explanation - stackoverflow.com/a/21434352/3055401
M.Doye - Thanks but that link just raises more questions in my mind. In the "route" I have .when('/Admin', { templateUrl: 'App/Views/Admin.html', controller: 'AdminController' }), then in the "controller" I have app.controller('AdminController', function ($scope, $http) {...}, and I used to have <div ng-controller="AdminController"> in the page. I still don't see how they can work together if I remove one of them, but I removed the ng-controller attribute and it seems to be working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.