是否可以在angularJS的$ httpBackend中接收URL参数?我想做的事情如下:
$httpBackend.whenGET('/api/v1/users/{userId}').respond(function (method, url, data) {
// somehow read {userId} and act differently depending on what the value is
return [200];
});
Run Code Online (Sandbox Code Playgroud)
拥有这个功能真的可以很容易地创建一个功能相当的模拟API(更容易开发前端应用程序而无需运行整个后端).
我是AngularJS的新手,无法通过REST更新对象。我正在使用PHP / Mysql后端(Slim框架)。
我能够检索(GET),创建(POST)一个新对象,但不能编辑(PUT)一个对象。这是代码:
我的表格:
<form name="actionForm" novalidate ng-submit="submitAction();">
Name: <input type="text" ng-model="action.name" name="name" required>
<input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
我的服务:
var AppServices = angular.module('AppServices', ['ngResource'])
AppServices.factory('appFactory', function($resource) {
return $resource('/api/main/actions/:actionid', {}, {
'update': { method: 'PUT'},
});
});
Run Code Online (Sandbox Code Playgroud)
app.js
var app = angular.module('app', ['AppServices'])
app.config(function($routeProvider) {
$routeProvider.when('/main/actions', {
templateUrl: 'partials/main.html',
controller: 'ActionListCtrl'
});
$routeProvider.when('/main/actions/:actionid', {
templateUrl: 'partials/main.html',
controller: 'ActionDetailCtrl'
});
$routeProvider.otherwise({redirectTo: '/main/actions'});
});
Run Code Online (Sandbox Code Playgroud)
controllers.js:
function ActionDetailCtrl($scope, $routeParams, appFactory, $location) {
$scope.action = appFactory.get({actionid: $routeParams.actionid});
$scope.addAction = function() {
$location.path("/main/actions/new");
}
$scope.submitAction …Run Code Online (Sandbox Code Playgroud)