javascript - 无法访问 angularjs 工厂属性

标签 javascript angularjs angularjs-service

我正在尝试建立一个 Angularjs 工厂, 我在单独的模块上编写脚本,然后注入(inject):

var app = angular.module('myApp', ['ngAnimate', 'ngTouch', 'myModule']);

但是当我尝试在运行阶段记录工厂的属性时,它是未定义的:

console.log(myFactory.update()); // undefined

这是我的工厂实现,是根据 angularjs 文档完成的:

    var myModule = angular.module('myModule', []);

    myModule.factory('myFactory', function() {
    return {
    controls: {
      'text_size'        : 1,           // text size level; default: 1; type: integer; options: [1-5]
      'underline'        : false,      // underline mode state; default: FALSE; type: boolean; options: [true (on), false (off)]
      'zoom'             : 1,         // zoom level; default: 1; type: integer; options: [1-5]
      'contrast'         : null,     // contrast mode; default: null; type: string, options: [null, 'dark', 'light']
      'background_color' : null,    //  background color; default: null; type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
      'headlines_color'  : null,   // headlines color; default: null; type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
      'text_color'       : null,  // text color; default: null; type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
      'focus_indicator'  : {     // focus indicator mode and color;
        'active'         : true,// default: true (on); type: boolean; options: [true (on), false (off)]
        'color'          : 'black', // default: black (change to match website style), type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
      },
      'media_controls'   : false,// default: false (change to match website style), type: boolean; options: [true (on), false (off)]
    },
    update: function(control, value) {
      this.controls[control] = value;
    }
  }
    });

也尝试过这样做:

var myModule = angular.module('myModule', []);

myModule.factory('myFactory', function() {
  var finalInstance = function() {
    this.controls = {
      'text_size'        : 1,           // text size level; default: 1; type: integer; options: [1-5]
      'underline'        : false,      // underline mode state; default: FALSE; type: boolean; options: [true (on), false (off)]
      'zoom'             : 1,         // zoom level; default: 1; type: integer; options: [1-5]
      'contrast'         : null,     // contrast mode; default: null; type: string, options: [null, 'dark', 'light']
      'background_color' : null,    //  background color; default: null; type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
      'headlines_color'  : null,   // headlines color; default: null; type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
      'text_color'       : null,  // text color; default: null; type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
      'focus_indicator'  : {     // focus indicator mode and color;
        'active'         : true,// default: true (on); type: boolean; options: [true (on), false (off)]
        'color'          : 'black', // default: black (change to match website style), type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
      },
      'media_controls'   : false,// default: false (change to match website style), type: boolean; options: [true (on), false (off)]
    };

    this.update = function(control, value) {
      this.controls[control] = value;
    };
  };

  return new finalInstance();
});

没有任何效果...

有什么建议吗?

最佳答案

我认为在这里使用服务是合适的(也可以使用工厂),因为您在更新的方法中引用了 this

此外,在记录方法时,您不会从更新方法返回任何内容,因此无论如何,它都会打印 undefined 直到您返回任何内容。我认为您应该返回控件以通过更新方法查看更新的控件列表。

代码

var myModule = angular.module('myModule', []);

myModule.service('myFactory', function() {
  var self = this; //this self will ensure you are accessing `this` correctly
  self.controls = {
    'text_size': 1, // text size level; default: 1; type: integer; options: [1-5]
    'underline': false, // underline mode state; default: FALSE; type: boolean; options: [true (on), false (off)]
    'zoom': 1, // zoom level; default: 1; type: integer; options: [1-5]
    'contrast': null, // contrast mode; default: null; type: string, options: [null, 'dark', 'light']
    'background_color': null, //  background color; default: null; type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
    'headlines_color': null, // headlines color; default: null; type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
    'text_color': null, // text color; default: null; type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
    'focus_indicator': { // focus indicator mode and color;
      'active': true, // default: true (on); type: boolean; options: [true (on), false (off)]
      'color': 'black', // default: black (change to match website style), type: string; options: ['black', 'darkgrey', 'grey', 'red', 'orange', 'yellow', 'green', 'lightblue', 'blue', 'purple', 'pink', null]
    },
    'media_controls': false, // default: false (change to match website style), type: boolean; options: [true (on), false (off)]
  };
  self.update = function(control, value) {
    self.controls[control] = value; //accessing correct controls object
    return self.controls; //returning controls
  };
});

关于javascript - 无法访问 angularjs 工厂属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36032678/

相关文章:

javascript - 如何检查 ul 标签的 li 中是否正在播放任何 YouTube 视频,然后相应地执行一些操作

javascript - 用于检查 Angular Bootstrap 模态范围的 Jasmine 单元测试

c# - Angular 如何在 WebApi asp.net mvc 中工作

angularjs - 在 Controller AngularJS 之间共享数据

http - 如何在 AngularJS 中执行 HTTP 选项请求?

javascript - `application.js` 不再包含 Rails 3.1 应用程序的 app/assets/javascripts 中的所有脚本

javascript - React 从状态数组中删除

javascript - PHP通知: Trying to get property of non-object

javascript - 使用angularjs获取动态创建的表行值的值

javascript - 无法从 $q 获取数据到作用域