[英]How can i use Restful in angularjs.I used ngResource but its not working .The js file nt executing if i used ngResource
var app = angular.module('app', ['ngResource']);
app.factory('UserFactory', function ($resource) {
return $resource('/com/vsoft/rest/users', {}, {
query: {
method: 'GET',
params: {},
isArray: false
}
});
});
app.controller('MyCtrl1', ['$scope', 'UserFactory', function ($scope, UserFactory) {
UserFactory.get({}, function (userFactory) {
$scope.firstname = userFactory.firstName;
$scope.lastname = userFactory.lastName;
});
});
}]);
i added above app in my html.But the app and angular-resource.js but my app.js is not exeuting. 我在我的html中添加了上面的应用程序,但是该应用程序和angular-resource.js但我的app.js没有执行。
If i removed ngResource module and $resource alert is coming.But if i used ngResource im nt getting alert. 如果我删除了ngResource模块并且$ resource警报来了。但是如果我使用ngResource im nt会得到警报。
Please help in this.If any one knows any Good Example to use Restful services with angularjs .Please Kindly send Url or code. 请帮助。如果有人知道任何很好的例子,可以在angularjs中使用Restful服务。请发送Url或代码。
Please help me. 请帮我。
i called{{firstname}} in my html but its not coming . 我在html中调用了{{firstname}},但没有成功。
I use a service for handling RESTful messages 我使用服务来处理RESTful消息
app.service('restService', function ($http, $log) {
'use strict';
var self = this;
var BASE_URL = "base/url/";
//First way how to do it
self.httpGet = function (url) {
$log.info("HTTP Get", url);
return postProcess($http({method: 'GET', url: BASE_URL + url}));
};
//Second way how to do it
self.httpPut = function (url, object) {
$log.info("HTTP Put", url);
return postProcess($http.put(BASE_URL + url, object));
};
self.httpPost = function (url, object) {
$log.info("HTTP Post", url);
return postProcess($http.post(BASE_URL + url, object));
};
self.httpDelete = function (url) {
$log.info("HTTP Delete", url);
return postProcess($http.delete(BASE_URL + url));
};
function postProcess(httpPromise) {
return httpPromise.then(function (response) {
if (response.status === 200) {
return response;
}
//Other than 200 is not ok (this is application specific)
failure(response);
}, function (response) {
failure(response);
});
}
/**
* Promise for failure HTTP codes
* @param response the HTTP response
*/
function failure(response) {
//Error handling
}
});
usable as 可用作
restService.httpGet("categories").then(function (response) {
categoryData = angular.fromJson(response.data);
//Broadcast an event to tell that the data is ready to be used
$rootScope.$broadcast("categoriesReady");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.