1

I'm using restangular to call external API. The GET request works only if I remove the single quotes from controller. Although, when I try to add an angular chart that fails, unless I add quotes back. So, one or the other works depending on if added or not. I need both to work so can use data from API for chart. Additionally, I'm not seeing any errors in browsers with or without the quotes.

This is partial app.js

var app = angular.module('mymod', ['ngRoute', 'ngResource', 'ui.bootstrap', 'chart.js', 'restangular'])
      .config(function($routeProvider, RestangularProvider) {
          $routeProvider
              .when('/', {
                  templateUrl: 'app/main/main.html',
                  controller: MainCtrl // <--- This is what I'm referring to
              })
              .otherwise({
                  redirectTo: '/'
              });

          // Set base url
          RestangularProvider.setBaseUrl('https://something.com/api/v1');
          // Auth details
          RestangularProvider.setDefaultHeaders({
              'Authorization': 'Token token=mytoken',
              'Content-Type': 'application/json',
          });
          // Format    
          RestangularProvider.setRequestSuffix('.json');
          // Data extractor
          RestangularProvider.setResponseExtractor(function(response, operation) {
              return response.incidents;
          });
      });

  // Graph Example
  app.controller('MainCtrl', function($scope) {
      $scope.labels = ["Download Sales", "In-Store Sales", "Mail-Order Sales"];
      $scope.data = [300, 500, 100];
  });

I added 'use strict'; to top of app.js and am now seeing this error: ReferenceError: i is not defined.
Which references the i in for loop below.

$scope.update = function(pages) {
for (i = 0; i < pages; i++) {
  Restangular.all("incidents").getList({offset:25 * i, sort_by: "created_on:desc"})
    .then(function(result) {
    Array.prototype.push.apply($scope.incidents,result);
    })
}
};

Also, I've been utilizing code example noted below and found a couple problems with it that i've fixed, but I'm new to Angular, so it's been difficult. reference: code example

8
  • 2
    It has to be 'MainCtrl' for sure.. Commented May 18, 2015 at 2:12
  • Can you add the single quotes around MainCtrl in your $routeProvider, and remove the word "app" in "app.controller", so just ".controller..." and see if this helps? Commented May 18, 2015 at 2:28
  • It seems to me that you're not showing all of the relevant code. If you can get the controller to work properly by referencing it w/a variable or function name (w/out the quotes around it), then you MUST have defined this variable/function somewhere. Yet your definition of the controller does not introduce such a variable. What gives? Commented May 18, 2015 at 2:50
  • regarding Restangular itself: you may also want to take a look at this SO post link which talks about different versions of AngularJS and what Restangular returns (changed from returning data to returning a promise). Additionally, see the original restangular repo here link and scroll down to "Supported Angular versions" for more info. Commented May 18, 2015 at 2:50
  • 1
    error: ReferenceError: i is not defined : you did not declare i as a var ? should be, e.g. for (var i =0; i < pages; i++) Commented May 18, 2015 at 3:44

0

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.