简体   繁体   English

无法访问angularjs工厂属性

[英]angularjs factory properties aren't accessible

I am trying to build an Angularjs factory, I'm writing the script on seperate module and then inject: 我试图建立一个Angularjs工厂,我在单独的模块上编写脚本,然后注入:

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

but when I try to log a factory's property in the run stage, it's undefined: 但是当我尝试在运行阶段记录工厂的属性时,它是未定义的:

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

Here is my factory implementation, did it according to the angularjs documentation: 这是我的工厂实现,是根据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;
    }
  }
    });

also tried to do it this way: 也尝试这样做:

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();
});

nothing works... 什么都行不通...

any suggestions? 有什么建议么?

I think using service would be appropriate here(could use factory as well), as you are messing up to having this reference inside updated method. 我认为在这里使用服务将是适当的(也可以使用工厂),因为您搞砸了在更新方法中包含this引用。

Also while logging method you are not returning anything from update method, so any case it is going to print undefined until you return anything. 同样,在记录方法时,您不会从更新方法中返回任何内容,因此在任何情况下,它都将打印undefined直到您返回任何内容。 I think you should return controls to see updated controls list from update method. 我认为您应该返回控件以从update方法查看更新的控件列表。

Code

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
  };
});

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

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