[英]RESTAngular with factory method is not working
Following the tips in Best practice of RestAngular , I am trying to follow the suggestions ( Restangular Service - Factory and exampleService ) and it's not working. 继在提示RestAngular的最佳实践 ,我想跟随建议(Restangular服务-工厂和exampleService),它不工作。
The following source code includes two sections one. 以下源代码包括两个部分。 Without Angular Factory, that works. 没有Angular Factory,那行得通。 And with Factory that is not working: 而与工厂不起作用:
<html ng-app="angularexample">
<head>
<script src="https://code.angularjs.org/1.3.14/angular.min.js"></script>
<script src="https://code.angularjs.org/1.3.14/angular-route.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/restangular/1.5.1/restangular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-Controller='MainCtrl'>
<div ng-repeat="user in users">
Name : {{user.name}}<br>
Age : {{user.age}}<br>
ID : {{user.id}}<br>
Change name:<input type="text" ng-model="user.name"/><button type="submit" ng-click="user.put()">Update</button><br/>
Remove ID:<input type="text" ng-model="user.id"/><button type="submit" ng-click="user.remove()">Delete</button><br/>
</div>
<div>
add new: <br/>
Name : <input type="text" ng-model="newUser.name"/><br/>
Age : <input type="text" ng-model="newUser.age"/><br/>
<button type="submit" ng-click="add()">add</button>
</div>
</div>
<div ng-Controller='ExampleCtrl'>
My Title {{title}} <br/>
<div ng-repeat="user in examples">
Name : {{user.name}}<br>
Age : {{user.age}}<br>
ID : {{user.id}}<br>
Change name:<input type="text" ng-model="user.name"/><button type="submit" ng-click="user.put()">Update</button><br/>
Remove ID:<input type="text" ng-model="user.id"/><button type="submit" ng-click="user.remove()">Delete</button><br/>
</div>
<div>
add new: <br/>
Name : <input type="text" ng-model="newUser.name"/><br/>
Age : <input type="text" ng-model="newUser.age"/><br/>
<button type="submit" ng-click="add()">add</button>
</div>
</div>
</body>
</html>
http://plnkr.co/edit/y22aBpcUwV1btrKB5jVM?p=preview http://plnkr.co/edit/y22aBpcUwV1btrKB5jVM?p=preview
Here is the error screen: 这是错误屏幕:
http://i57.tinypic.com/334rfqu.jpg http://i57.tinypic.com/334rfqu.jpg
The working reference is pictured as my REST service is not hosted . 由于未托管我的REST服务,因此工作参考如图所示。 Yet my REST service is just having two fields namely - name and age. 但是我的REST服务只有两个字段-名称和年龄。
/**
* User.js
*
* @description :: This is the sample Model for Sails JS to create REST API.
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = {
attributes: {
name : {
type : 'string'
},
age : {
type : 'integer'
}
}
};
What could be an issue in my code? 我的代码中可能有什么问题? I would like to follow the best practices to build a big application. 我想遵循最佳实践来构建大型应用程序。
The code fails because your service methods do not return anything. 代码失败,因为您的服务方法未返回任何内容。 Update them as follows: 如下更新它们:
// get examples from server by using Restangular
function getExamples(){
return Restangular.all('user').getList();
}
You are calling the then method without returning the associated promise from your factory methods, you should return the promise, like 您在调用then方法时未从工厂方法中返回关联的promise,因此应返回promise,例如
// get examples from server by using Restangular
function getExamples(){
return Restangular.all('user').getList();
}
// get example with given id from server by using Restangular
function getExample(exampleId){
return Restangular.one('user', exampleId).get();
}
Now the methods will make the request and return the assoiciated promise on which you can call the then method. 现在,这些方法将发出请求并返回相关的promise,您可以在其上调用then方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.