您如何管理与 AngularJS 的模型关系?在 Ember 中很容易,但如何在 AngularJS 中处理它而不产生一堆乱码?
最佳答案
我使用 https://github.com/klederson/ModelCore/它非常简单易用
var ExampleApp = angular.module('ExampleApp', ['ModelCore']); //injecting ModelCore
ExampleApp.factory("Users",function(ModelCore) {
return ModelCore.instance({
$type : "Users", //Define the Object type
$pkField : "idUser", //Define the Object primary key
$settings : {
urls : {
base : "http://myapi.com/users/:idUser",
}
},
$myCustomMethod : function(info) { //yes you can create and apply your own custom methods
console.log(info);
}
});
});
然后我只需要注入(inject)我的 Controller 并使用它
function MainCrtl($scope, Users) {
//Setup a model to example a $find() call
$scope.AllUsers = new Users();
//Get All Users from the API
$scope.AllUsers.$find().success(function() {
var current;
while(current = $scope.AllUsers.$fetch()) { //fetching on masters object
console.log("Fetched Data into Master Object",$scope.AllUsers.$toObject()) //reading fetched from master
//or just get the fetched object itself
console.log("Real fetched Object",current.$toObject())
}
});
关于javascript - 在 AngularJS 中管理模型关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14708804/