2015-03-02 44 views
0

我不明白爲什麼我的工廠與REST API通信不起作用。 我收到以下錯誤:TypeError:無法讀取未定義的屬性「協議」

TypeError: Cannot read property 'protocol' of undefined 

TypeError: Cannot read property 'message' of undefined 

todo.controller.js

'use strict'; 
angular 
    .module('app') 
    .controller('TodoController', ['$scope', 'todoFactory', function ($scope, todoFactory) { 

     getTodos(); 

     function getTodos() { 
      todoFactory.all() 
       .success(function (todos) { 
        $scope.todos = todos; 
       }) 
       .error(function (error) { 
        $scope.status = 'Unable to load todo data: ' + error.message; 
       }); 
     } 

    }]); 

todo.factory.js

'use strict'; 
angular.module('app') 
    .constant('API_URI', 'http://localhost:8080/api/todo') 
    .factory('todoFactory', ['$http', function($http, API_URI) { 

     var todoFactory = {}; 

     function getUrl() { 
      return API_URI; 
     } 

     function getUrlForId(itemId) { 
      return getUrl + itemId; 
     } 

     todoFactory.all = function() { 
      return $http.get(getUrl()); 
     }; 

     todoFactory.fetch = function (id) { 
      return $http.get(getUrlForId(id)); 
     }; 

     todoFactory.add = function (todo) { 
      return $http.post(getUrl(), todo); 
     }; 

     todoFactory.update = function (todo) { 
      return $http.put(getUrlForId(id), todo) 
     }; 

     todoFactory.delete = function (id) { 
      return $http.delete(getUrlForId(id)); 
     }; 

     return todoFactory; 
    }]); 

但是,如果我將調用簡單$ http.get(東西)在工作控制器:

$http.get('http://localhost:8080/api/todo'). 
     success(function(data) { 
      $scope.todos = data; 
     }); 

我做什麼毛病我廠使用比較簡單直接$ http.get()在控制器?

回答

1

有了這個語法(縮小支持):

.factory('todoFactory', ['$http', function($http, API_URI) { 

您需要添加'API_URI'作爲一個字符串,以及(旁邊'$http'

.factory('todoFactory', ['$http', 'API_URI', function($http, API_URI) { 

否則,關注的是注入了API_URI參數。

+0

歐,天哪,其實我的不好。感謝名單=) – user1376885 2015-03-02 22:47:15

相關問題