0

I got this error with below code:

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

my partial front end that used 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>

My app.js file

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;
        });
    }
}

I pass the $scope and expect it to work, but I got an undienfed error at line FreshlyPressed.getBlogs($scope);

1 Answer 1

1

Looks like your dependency injection order is incorrect

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

change to

App.controller("AppCtrl", ["$scope", "$log","FreshlyPressed", AppCtrl]);
Sign up to request clarification or add additional context in comments.

2 Comments

does the order matter?
Yes it does matter. :) As per the arguments passed FreshlyPressed is referring to $log, however $log doesn't have any method called getBlogs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.