最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Importing JSON file into Angular application running on Grunt server - Stack Overflow

programmeradmin0浏览0评论

I am following the basic Angular tutorial and need to include a JSON file in it. I kickstarted my application with Yeoman and it is running on grunt.

var phonecatApp = angular.module('phonecatApp', []);

phonecatApp.controller('PhoneListCtrl', function PhoneListCtrl($scope) {

  $http.get('phones/phones.json').success(function(data) {
    $scope.phones = data;
  });

});

However when I go to localhost:9000 I get a bunch of console errors:

ReferenceError: $http is not defined
    at new PhoneListCtrl (http://localhost:9000/scripts/controllers/main.js:17:3)
    at invoke (http://localhost:9000/bower_ponents/angular/angular.js:3000:28)
    at Object.instantiate (http://localhost:9000/bower_ponents/angular/angular.js:3012:23)
    at http://localhost:9000/bower_ponents/angular/angular.js:4981:24
    at http://localhost:9000/bower_ponents/angular/angular.js:4560:17
    at forEach (http://localhost:9000/bower_ponents/angular/angular.js:137:20)
    at nodeLinkFn (http://localhost:9000/bower_ponents/angular/angular.js:4545:11)
    at positeLinkFn (http://localhost:9000/bower_ponents/angular/angular.js:4191:15)
    at positeLinkFn (http://localhost:9000/bower_ponents/angular/angular.js:4194:13)
    at publicLinkFn (http://localhost:9000/bower_ponents/angular/angular.js:4096:30) 

Any help would be appreciated!

I am following the basic Angular tutorial and need to include a JSON file in it. I kickstarted my application with Yeoman and it is running on grunt.

var phonecatApp = angular.module('phonecatApp', []);

phonecatApp.controller('PhoneListCtrl', function PhoneListCtrl($scope) {

  $http.get('phones/phones.json').success(function(data) {
    $scope.phones = data;
  });

});

However when I go to localhost:9000 I get a bunch of console errors:

ReferenceError: $http is not defined
    at new PhoneListCtrl (http://localhost:9000/scripts/controllers/main.js:17:3)
    at invoke (http://localhost:9000/bower_ponents/angular/angular.js:3000:28)
    at Object.instantiate (http://localhost:9000/bower_ponents/angular/angular.js:3012:23)
    at http://localhost:9000/bower_ponents/angular/angular.js:4981:24
    at http://localhost:9000/bower_ponents/angular/angular.js:4560:17
    at forEach (http://localhost:9000/bower_ponents/angular/angular.js:137:20)
    at nodeLinkFn (http://localhost:9000/bower_ponents/angular/angular.js:4545:11)
    at positeLinkFn (http://localhost:9000/bower_ponents/angular/angular.js:4191:15)
    at positeLinkFn (http://localhost:9000/bower_ponents/angular/angular.js:4194:13)
    at publicLinkFn (http://localhost:9000/bower_ponents/angular/angular.js:4096:30) 

Any help would be appreciated!

Share Improve this question asked Oct 9, 2013 at 17:14 The Whiz of OzThe Whiz of Oz 7,0439 gold badges52 silver badges91 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

It may be better for you to include the json file in a factory service. That way you can cache it and continue to use it with different controllers.

I had a similar issue and resolved it like so...

var App = angular.module('App', []);

// Setting up a service to house our json file so that it can be called by the controllers
App.factory('service', function($http) {
    var promise;
    var jsondata = {
        get: function() {
            if ( !promise ) {
                var promise =  $http.get('src/data_json.js').success(function(response) {
                    return response.data;
                });
                return promise;
            }
        }
    };
    return jsondata;
});




App.controller('introCtrl', function (service , $scope) {
    service.get().then(function(d) {
        $scope.header = d.data.PACKAGE.ITEM[0]
    })
});

App.controller('secondCtrl', function (service , $scope) {
    service.get().then(function(d) {
        $scope.title = d.data.PACKAGE.ITEM[1]
    })
});

Add $http as dependency, next to $scope in your function PhoneListCtrl($scope, $http) {}

发布评论

评论列表(0)

  1. 暂无评论