Javascript AngularJS中的模型关系管理

Javascript AngularJS中的模型关系管理,javascript,orm,angularjs,Javascript,Orm,Angularjs,您如何管理与AngularJS的模型关系?在Ember中这很容易,但是如何在AngularJS中处理它而不产生一堆混乱的代码呢?Angular没有像Ember data那样的“数据存储”层,但是如果您愿意,您可以集成任何现有的ORM-我使用它非常简单易用 var ExampleApp = angular.module('ExampleApp', ['ModelCore']); //injecting ModelCore ExampleApp.factory("Users",function(M

您如何管理与AngularJS的模型关系?在Ember中这很容易,但是如何在AngularJS中处理它而不产生一堆混乱的代码呢?

Angular没有像Ember data那样的“数据存储”层,但是如果您愿意,您可以集成任何现有的ORM-

我使用它非常简单易用

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);
    }
  });
});
然后我只需要注入我的控制器并使用它

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())
    }
  });

你能举个例子吗?在AngularJS中,您使用javascript对象进行操作,因此您可以直接引用其他对象,然后
$watch
它们(不要忘记将
true
设置为第二个参数)。还有依赖项注入,因此您可以注入依赖对象(注意
value
methodofmodule)