我是 AngularJs 的新手,我正在尝试用 ngResource 编写一个工厂来进行 Rest 调用
这是我的代码
工厂
'use strict';
angular.module('restPracticeApp')
.factory('myFactory',['$resource', function () {
console.log('hi from factory')
function myFactory($resource) {
var myUrl = 'api/theUrl';
return $resource(myUrl,{} , {
'query': { method: 'GET', isArray: true},
'get': { method: 'GET'}
});
}
}]);
控制器 :
'use strict';
angular.module('restPracticeApp')
.controller('AboutCtrl',['$scope','$resource','$http','myFactory' ,
function ($scope,$resource,$http,myFactory) {
$scope.theLink = theLink;
function theLink(theName) {
$http({
method: 'GET',
url: "http://localhost:9000/api/theUrl",
data: $scope.theName
})
.then(
function successCallback(response) {
console.log(response);
$scope.result=response;
},
function errorCallback(response) {
console.log(response);
$scope.result=response;
});
}
}]);
并在 HTML
<input type="text" ng-model="theName">
<button class="btn btn-block" ng-click="theLink(theName)">yo</button>
我正在尝试返回工厂,但它仍然说“提供者'myFactory'必须从 $get 工厂方法返回一个值。”
请帮帮我。我会很感激