我試圖建立一個Angularjs工廠, 我在寫單獨的模塊上的腳本,然後注入:angularjs工廠屬性是不可訪問
var app = angular.module('myApp', ['ngAnimate', 'ngTouch', 'myModule']);
但是當我嘗試登錄工廠的財產運行階段,它是不確定的:
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;
}
}
});
:
console.log(myFactory.update()); // undefined
這裏是我的工廠實現,根據angularjs文檔做到了
也試圖做這種方式:
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'服務的,我說使用'服務'這裏將是更好的辦法 –
嘿是它就像你正在上調用工廠對象的更新方法未定義?你有沒有在你的代碼的更新方法添加return語句? – Harpreet