Som*_*gOn 17 angularjs angularjs-factory
我正在遵循官方AngularJS文档中的教程,我想知道是否可以向Phone工厂添加另一个功能,以便我可以更好地组织代码.他们已经声明了一个"查询"函数,但是如果我想添加一个引用不同url的query2函数...比如说phones2 /:phoneName.json呢?
工厂申报:
var phonecatServices = angular.module('phonecatServices', ['ngResource']);
phonecatServices.factory('Phone', ['$resource',
function($resource){
return $resource('phones/:phoneId.json', {}, {
query: {method:'GET', params:{phoneId:'phones'}, isArray:true}
});
}]);
Run Code Online (Sandbox Code Playgroud)
我已经尝试过很多东西,但是它们似乎没有用到:s
这个答案似乎在正确的轨道上,但每个工厂功能的语法与上述工厂不完全匹配.
有点像:
phonecatServices.factory('Phone', ['$resource',
function($resource){
return {
query: ...
query2: ...
}
}]);
Run Code Online (Sandbox Code Playgroud)
Abh*_*eet 25
其中一个例子是: 演示链接
angular.module('services', []).factory('factoryName', ["$filter",
function($filter) {
var method1Logic = function(args) {
//code
};
var method2Logic = function(args) {
//code
};
return {
method1: method1Logic,
method2: method1Logic
};
}
]).controller('MainController', ["$scope", "$rootScope", "$filter", "factoryName", function ($scope, $rootScope, $filter,factoryName) {
$scope.testMethod1 = function(arg){
$scope.val1 = factoryName.method1(arg);
};
$scope.testMethod2 = function(arg){
$scope.val2 = factoryName.method2(arg);
};
}]);
Run Code Online (Sandbox Code Playgroud)
甚至有一个更好的版本Opinionated版本:参考
function AnotherService () {
var AnotherService = {};
AnotherService.someValue = '';
AnotherService.someMethod = function () {
};
return AnotherService;
}
angular
.module('app')
.factory('AnotherService', AnotherService);
Run Code Online (Sandbox Code Playgroud)
Som*_*gOn 14
这是服务代码:
myServices.factory('Auth', ['$resource',
function($resource){
return {
Login: $resource(serviceURL + 'login', {}, { go: { method:'POST', isArray: false }}),
Logout: $resource(serviceURL + 'logout', {}, { go: { method:'POST', isArray: false }}),
Register: $resource(serviceURL + 'register', {}, { go: { method:'POST', isArray: false }}),
};
}
]);
Run Code Online (Sandbox Code Playgroud)
从我的控制器我只需添加go()函数调用,使其工作:
Auth.Login.go({ username: $scope.username, password: $scope.password },
Run Code Online (Sandbox Code Playgroud)
我想我可以在方法之后命名go函数并将其命名为"post()"而不是为了清晰...
| 归档时间: |
|
| 查看次数: |
23059 次 |
| 最近记录: |