为什么这个带有可变对象名称的 AngularJs 服务返回一个未定义的对象? [英] Why does this AngularJs Service with variable object name return an undefined object?

查看:18
本文介绍了为什么这个带有可变对象名称的 AngularJs 服务返回一个未定义的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

我想使用此服务在我的 web 应用程序中共享一些属性.该服务内部应该有一些对象,控制器应该能够获取和设置这些对象.

I'd like to use this service to share some properties inside my webApp. This service should have some object inside and the controllers should be able to get and set those objects.

JS:

var oneApp = angular.module('myApp', ['ui.bootstrap', 'ngTouch', 'ngAnimate'])
  .service('sharedProps', function() {
    var anObjNameDest = '';
    var anObjPropName = '';

    this.progressBarDynTyped00 = {
      dynamic: 0,
      dynMultiplier: 10,
      max: 100
    };

    var progressBarDynTyped00 = {
      dynamic: 1000,
      dynMultiplier: 10010,
      max: 100100
    };


    var test = this.progressBarDynTyped00;

    var test2 = progressBarDynTyped00;

    return {
      getDynObject: function(anObjNameDest) {
        return this[anObjNameDest];
      },
      getDynObjectVal: function(anObjNameDest, anObjPropName) {
        return this[anObjNameDest][anObjPropName];
      },
      setDynObjectVal: function(anObjNameDest, anObjPropName, value) {
        this[anObjNameDest][anObjPropName] = value;
      },
      setDynObject: function(anObjNameDest, ObjSrc) {
        this[anObjNameDest] = ObjSrc;
      }
    }
  });


oneApp.directive("progressBarDynTyped", function() {
    return {
      restrict: "E",
      templateUrl: "aTemplateUrl.html",
      controller: function(sharedProps) {

        this.test = sharedProps.getDynObject('progressBarDynTyped00');

        this.dynMultiplier 
                = sharedProps.getDynObjectVal('progressBarDynTyped00', 'dynMultiplier');
      },
      controllerAs: "progressBarDynTyped00"
    };

  });

问题:

在上面的代码中,我有一个服务,其中有两个测试对象(以前只有一个,第二个是为了测试目的而添加的),这些测试对象应该从这个服务返回给一些控制器函数,就像在progressBarDynTyped00"控制器中一样,因为我需要在这些对象中获取和设置一些值.

in the code above I have a service where there are two test Objects (previously there were only one of those, the second one was added for test purpose), those test objects should be returned from this service to some contrellers functions, like in the "progressBarDynTyped00" controller, because I need to get and set some values inside those objects.

从服务中调用所有重新调整的函数总是给我一个未定义的对象或无法读取未定义的属性 'dynMultiplier'".

Calling all of the retuned functions from the service gave me always an Undefined object or a "Cannot read property 'dynMultiplier' of undefined".

问题:

有没有办法返回一个动态对象,将对象的名称传递给服务函数?

Is there a way to return a dynamic object passing the name of the object to the service function?

谢谢大家.

推荐答案

我认为您对在 Angular 中创建服务的两种方式感到困惑:使用 service 关键字和 factory 关键字

I think you are confused between two ways of creating service in Angular: using service keyword and factory keyword

对于 service 关键字,应该通过将其分配给 this 来使公共功能可用:

For service keyword, the public functions are supposed to be made available by assigning it to this:

angular.module('myApp', ['ui.bootstrap', 'ngTouch', 'ngAnimate']).service('sharedProps', function () {

    this.getDynObject = function (anObjNameDest) {
       //...
    };

}]);

它不期望有返回值.所以对于上面的代码,没有要调用的公共函数,这就解释了错误

And it doesn't expect to have return value. So with the code above, there's no public functions to be called, and that explains the error

这篇关于为什么这个带有可变对象名称的 AngularJs 服务返回一个未定义的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆