AngularJS:从工厂,我如何调用另一个功能

前端之家收集整理的这篇文章主要介绍了AngularJS:从工厂,我如何调用另一个功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须将getTemplates函数从返回中移出还是什么?

示例:我不知道用“XXXXXXX”替换(我尝试过“this / self / templateFactory”等)…):

  1. .factory('templateFactory',[
  2. '$http',function($http) {
  3.  
  4. var templates = [];
  5.  
  6. return {
  7. getTemplates : function () {
  8. $http
  9. .get('../api/index.PHP/path/templates.json')
  10. .success ( function (data) {
  11. templates = data;
  12. });
  13. return templates;
  14. },delete : function (id) {
  15. $http.delete('../api/index.PHP/path/templates/' + id + '.json')
  16. .success(function() {
  17. templates = XXXXXXX.getTemplates();
  18. });
  19. }
  20. };
  21. }
  22. ])
通过做模板= this.getTemplates();您指的是尚未实例化的对象属性.

相反,您可以逐渐填充对象:

  1. .factory('templateFactory',['$http',function($http) {
  2. var templates = [];
  3. var obj = {};
  4. obj.getTemplates = function(){
  5. $http.get('../api/index.PHP/path/templates.json')
  6. .success ( function (data) {
  7. templates = data;
  8. });
  9. return templates;
  10. }
  11. obj.delete = function (id) {
  12. $http.delete('../api/index.PHP/path/templates/' + id + '.json')
  13. .success(function() {
  14. templates = obj.getTemplates();
  15. });
  16. }
  17. return obj;
  18. }]);

猜你在找的Angularjs相关文章