本文介绍了$ Resource& $ http都在工厂中未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工厂中创建了TemplateService,我调用GetBracket它将调用GetBracket From控制器。但在GetBracket功能中需要$ resouce& $ http服务在工厂中未定义。我在我的应用程序中添加了ngResource依赖性。和html页面中的

I create TemplateService as factory in I call GetBracket it will call GetBracket From controller. But in GetBracket Function will required $resouce & $http service is undefined in factory. I add ngResource dependancy in my app. And Refrence of

angular.js

angular-resource.js

的参考定义了ng-app和ng-controller指令。



我尝试过:



var app = angular.module('myApp',['ngResource']);

app.controller('myCtrl',['$ scope','$ http',' TemplateService',函数($ scope,$ http,TemplateService){

$ scope.firstName =;

$ scope.lastName =;

$ scope.Click = function(){

TemplateService.GetBracket()。then(function(accountList){

$ scope.firstName = accountList;

});

}

}]);

app.factory(TemplateService,函数($ http,$ resource){

返回{

GetBracket:GetBracket

};

函数GetBracket($ http ,$ resource){

返回$ resource('/ Home / Name',{})。get()。$ promise();

}



});

in the html page & defined ng-app and ng-controller directive.

What I have tried:

var app = angular.module('myApp', ['ngResource']);
app.controller('myCtrl', ['$scope', '$http', 'TemplateService', function ($scope, $http, TemplateService) {
$scope.firstName = "";
$scope.lastName = "";
$scope.Click = function () {
TemplateService.GetBracket().then(function (accountList) {
$scope.firstName = accountList;
});
}
}]);
app.factory("TemplateService", function ($http, $resource) {
return {
GetBracket: GetBracket
};
function GetBracket($http, $resource) {
return $resource('/Home/Name', {}).get().$promise();
}

});

推荐答案


angular.js

angular-resource.js

的参考定义了ng-app和ng-controller指令。



我尝试过:



var app = angular.module('myApp',['ngResource']);

app.controller('myCtrl',['

in the html page & defined ng-app and ng-controller directive.

What I have tried:

var app = angular.module('myApp', ['ngResource']);
app.controller('myCtrl', ['



这篇关于$ Resource& $ http都在工厂中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:37