在我的情况下,AngularJS undefined不是函数错误

时间:2014-11-24 05:18:10

标签: javascript angularjs

我在下面的代码中遇到了这个错误:

TypeError: undefined is not a function
    at Scope.AppCtrl.$scope.refresh

我的部分前端使用了ng-repeat

<ion-content>
        <div class="card list" data-ng-repeat="p in posts">
        <div class="item item-avatar-left">
            <img data-ng-src="{{p.author.avatar_URL}}">
            <h2>{{p.author.nice_name}}</h2>
            <p><a href="{{p.author.URL}}"></a>{{p.author.URL}}</p>
        </div>
        <div class="item item-body">
        <h1>{{p.title}}</h1>
        <p>{{p.content}}</p>
        </div>
    </div>
</ion-content>

我的app.js文件

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

App.service("FreshlyPressed", ["$http","$log",FreshlyPressed]);
App.controller("AppCtrl", ["$scope", "FreshlyPressed", "$log", AppCtrl]);

function AppCtrl($scope, $log, FreshlyPressed){
  $scope.posts = [];
  $scope.refresh = function(){
    FreshlyPressed.getBlogs($scope); 
  }
}

function FreshlyPressed($http, $log){
    this.getBlogs = function($scope){
        $http.jsonp("https://public-api.wordpress.com/rest/v1/freshly-pressed?callback=JSON_CALLBACK")
        .success(function(result){
            $scope.posts = result.posts;
        });
    }
}

我通过了$ scope并期望它能够正常工作,但我在第FreshlyPressed.getBlogs($scope);行收到了一个未发现的错误

1 个答案:

答案 0 :(得分:1)

看起来您的依赖注入顺序不正确

App.controller("AppCtrl", ["$scope", "FreshlyPressed", "$log", AppCtrl]);

更改为

App.controller("AppCtrl", ["$scope", "$log","FreshlyPressed", AppCtrl]);