如何在控制器中包含angular.js服务的数据?(How to include an angular.js service's data in a controller?)

编程入门 行业动态 更新时间:2024-10-28 18:23:57
如何在控制器中包含angular.js服务的数据?(How to include an angular.js service's data in a controller?)

我创建了一个从服务器获取数据的服务。 浏览器工具确认数据已正确拾取。

angular. module('karassApp'). factory('Person', ['$rootScope', '$http', function($rootScope, $http){ var persons = []; var loaded = false; // returns a promise that resolves to the persons. var load = function(){ return $http.get('/get_persons').then(function(response){ persons = response.data.map(function(person){ return { id: person.id, fullname: person.fullname, birthdate: person.birthdate ? new Date(person.birthdate) : null, deathdate: person.deathdate ? new Date(person.deathdate) : null, description: person.description, picture: person.picture ? person.picture : '/client/views/person.png' }; }); $rootScope.$broadcast('Persons.update'); loaded = true; return persons; }); }; return { load: load, persons: persons }; }]);

接下来,我有控制器应该使用该服务。

angular. module('karassApp'). component('personList', { templateUrl: 'client/views/person-list.template.html', controller: ['$scope', 'Person', function PersonListController($scope, Person){ var self = this; Person.load().then(function(){ $scope.persons = Person.persons; }); ...

此时,代码失败,$ scope.persons结束为空。 我等待承诺解决的人应确保将数据加载到人员中。 我还认为该服务是一个单例,这意味着在第一次调用Person.load()之后应该填充person变量。

先谢谢你,乔希

更新我试图合并答案中的代码,现在看起来像这样:

angular. module('karassApp'). factory('Person', ['$http', '$q', function($http, $q){ var persons = []; // returns a promise that resolves to the persons. var load = function(){ return $q(function(resolve, reject){ $http.get('/get_persons').then(function(response){ persons = response.data.map(function(person){ return { id: person.id, fullname: person.fullname, birthdate: person.birthdate ? new Date(person.birthdate) : null, deathdate: person.deathdate ? new Date(person.deathdate) : null, description: person.description, picture: person.picture ? person.picture : '/client/views/person.png' }; }); resolve(persons); }); }); };

...

I've created a service that fetches data from the server. The browser tools confirm that the data is picked up correctly.

angular. module('karassApp'). factory('Person', ['$rootScope', '$http', function($rootScope, $http){ var persons = []; var loaded = false; // returns a promise that resolves to the persons. var load = function(){ return $http.get('/get_persons').then(function(response){ persons = response.data.map(function(person){ return { id: person.id, fullname: person.fullname, birthdate: person.birthdate ? new Date(person.birthdate) : null, deathdate: person.deathdate ? new Date(person.deathdate) : null, description: person.description, picture: person.picture ? person.picture : '/client/views/person.png' }; }); $rootScope.$broadcast('Persons.update'); loaded = true; return persons; }); }; return { load: load, persons: persons }; }]);

Next up, I have controller that's supposed to be consuming the service.

angular. module('karassApp'). component('personList', { templateUrl: 'client/views/person-list.template.html', controller: ['$scope', 'Person', function PersonListController($scope, Person){ var self = this; Person.load().then(function(){ $scope.persons = Person.persons; }); ...

At this point, the code fails and $scope.persons ends up empty. I waiting for the person promise to resolve should ensure that the data is loaded into persons. I also thought that the service was a singleton, which means the persons variable should be filled after the first call to Person.load().

Thanks in advance, Josh

Update I tried to incorporate the code from the answer, and it looks like this now:

angular. module('karassApp'). factory('Person', ['$http', '$q', function($http, $q){ var persons = []; // returns a promise that resolves to the persons. var load = function(){ return $q(function(resolve, reject){ $http.get('/get_persons').then(function(response){ persons = response.data.map(function(person){ return { id: person.id, fullname: person.fullname, birthdate: person.birthdate ? new Date(person.birthdate) : null, deathdate: person.deathdate ? new Date(person.deathdate) : null, description: person.description, picture: person.picture ? person.picture : '/client/views/person.png' }; }); resolve(persons); }); }); };

...

最满意答案

我想到了。 我有正确的想法,但我需要让人们能够将其传递到下一个。

服务:

angular. module('karassApp'). factory('Person', ['$rootScope', '$http', '$q', function($rootScope, $http, $q){ var persons = []; // returns a promise that resolves to the persons. var load = function(){ return $http.get('/get_persons').then(function(response){ persons = response.data.map(function(person){ return { id: person.id, fullname: person.fullname, birthdate: person.birthdate ? new Date(person.birthdate) : null, deathdate: person.deathdate ? new Date(person.deathdate) : null, description: person.description, picture: person.picture ? person.picture : '/client/views/person.png' }; }); return persons; }); };

控制器:

angular. module('karassApp'). component('personList', { templateUrl: 'client/views/person-list.template.html', controller: ['$scope', 'Person', function PersonListController($scope, Person){ var self = this; Person.load().then(function(persons){ self.persons = persons; });

I figured it out. I had the right idea, but I needed to return persons to be able to pass it to the next then.

service:

angular. module('karassApp'). factory('Person', ['$rootScope', '$http', '$q', function($rootScope, $http, $q){ var persons = []; // returns a promise that resolves to the persons. var load = function(){ return $http.get('/get_persons').then(function(response){ persons = response.data.map(function(person){ return { id: person.id, fullname: person.fullname, birthdate: person.birthdate ? new Date(person.birthdate) : null, deathdate: person.deathdate ? new Date(person.deathdate) : null, description: person.description, picture: person.picture ? person.picture : '/client/views/person.png' }; }); return persons; }); };

controller:

angular. module('karassApp'). component('personList', { templateUrl: 'client/views/person-list.template.html', controller: ['$scope', 'Person', function PersonListController($scope, Person){ var self = this; Person.load().then(function(persons){ self.persons = persons; });

更多推荐

本文发布于:2023-07-17 10:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142876.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器中   数据   如何在   angular   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!