简体   繁体   English

将数据从Angular 1.x Factory发送到Angular 1.x控制器

[英]Sending data from Angular 1.x Factory to Angular 1.x controller

I have a service defined which do the db related queries/updates. 我定义了一项服务,该服务执行数据库相关的查询/更新。 I have defined the controller which does the data parsing for the angular elements by getting the objects from the service. 我定义了一个控制器,该控制器通过从服务中获取对象来对角度元素进行数据解析。 I would like to keep each scope different 我想保持每个范围不同

How can I pass the data from service to controller using ngResource. 如何使用ngResource将数据从服务传递到控制器。

Sample Service: 样品服务:

app.factory("ioHomeService", ["$rootScope","$resource", function($rootScope,$resource) {
   var svc = {};
   var home = $resource('/home/getAll');
   var dbData= home.get();
   svc.getRooms = function() {
       return dbData;
   };
    return svc;
}]);

Sample Controller: 样品控制器:

app.controller("homeCtrl",["$scope","$mdDialog","ioHomeService",function($scope,$mdDialog,ioHome){
    $scope.dbData = ioHome.getRooms();
    //Here UI specific objects/data is derived from dbData
}]);

After the DB is queried and the results are avialble the dbData in service is reflecting the data from DB, but the Controller cannot get that data 在查询数据库并且结果不正确之后,服务中的dbData反映了来自数据库的数据,但是Controller无法获取该数据

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). 重要的是要意识到调用$ resource对象方法会立即返回空引用(对象或数组取决于isArray)。 Once the data is returned from the server the existing reference is populated with the actual data. 从服务器返回数据后,将使用实际数据填充现有引用。 This is a useful trick since usually the resource is assigned to a model which is then rendered by the view. 这是一个有用的技巧,因为通常资源会分配给模型,然后由视图呈现。 Having an empty object results in no rendering, once the data arrives from the server then the object is populated with the data and the view automatically re-renders itself showing the new data. 如果对象为空,则不会呈现任何内容,一旦数据从服务器到达,该对象就会被数据填充,并且视图会自动重新渲染以显示新数据。 This means that in most cases one never has to write a callback function for the action methods. 这意味着在大多数情况下,不必为动作方法编写回调函数。

From https://docs.angularjs.org/api/ngResource/service/ $resource 来自https://docs.angularjs.org/api/ngResource/service/ $ resource

Since the 'ioHome.getRooms();' 由于'ioHome.getRooms();' is being called before the $resource has returned the data you are getting dbData as an empty reference 在$ resource返回您正在获取dbData的数据作为空引用之前被调用

app.factory("ioHomeService", ["$rootScope","$resource", function($rootScope,$resource) {
   var svc = { 
     dbData : {}
   };
   var home = $resource('/home/getAll');
   var svc.dbData.rooms = home.get();
   return svc;
}]);

Controller 控制者

app.controller("homeCtrl",["$scope","$mdDialog","ioHomeService",function($scope,$mdDialog,ioHome){
    $scope.dbData  = ioHome.dbData;

    //You can access the rooms data using $scope.dbData.roooms

    //Here UI specific objects/data is derived from dbData
}]);

You would have to return the service object , like so : 您将必须返回服务对象,如下所示:

app.factory("ioHomeService", ["$rootScope","$resource",   function($rootScope,$resource) {
var svc = {};
var home = $resource('/home/getAll');
var dbData= home.get();
svc.getRooms = function() {
   return dbData;
};
return svc; //here
}]);

Currently the getRooms method is not visible to your controller. 当前, getRooms方法对控制器不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM