Edison Edison - 1 month ago 11
AngularJS Question

Cannot read property 'then' of undefined in angularjs

i was trying to make a simple app in ionic with asp.net mvc rest framework.but am getting


Cannot read property 'then' of undefined



error.here is my app.js code.its can anyone help..?.the controller part shows error

angular.module('starter', ['ionic', 'starter.controllers', ' starter.services'])
.constant('ApiEndpoint', {
url: 'http://localhost:49316/api'
})
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
});
})
angular.module('starter.services', [])
factory('Api', function($http, ApiEndpoint) {
console.log('ApiEndpoint', ApiEndpoint)
var getApiData = function() {
$http.get(ApiEndpoint.url + '/product')
.success(function(data) {

})
.error(function(error) {
})
}

return {
getApiData: getApiData
};
})
angular.module('starter.controllers', [])
.controller('ExampleController', function($scope, Api) {
$scope.getData = function() {
Api.getApiData()
.then(function(result) {
$scope.data = result.data;
})
}
});

Answer

You are already returning a promise in your below function.

var getApiData = function() {
    $http.get(ApiEndpoint.url + '/product')
        .success(function(data) {

        })
        .error(function(error) {
        })
 }

Change the below function to below like this.

var getApiData = function() {
    return $http.get(ApiEndpoint.url + '/product')
}