Я пытаюсь построить Angularjs завод, Я пишу сценарий на отдельном модуле и затем вводят:angularjs Свойства фабрики не доступны
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', которые вы используете в' this.controls [управления] = значение; 'не же, что' this' службы, я бы сказал, с помощью 'службы 'здесь был бы лучший подход –
Эй, как вы получаете undefined при вызове метода обновления фабричного объекта? Вы добавили оператор возврата в метод обновления в свой код? – Harpreet