ltr*_*npr 8 javascript ember.js
这是我的第一个Ember.js应用程序.我正在构建一个多项选择题(最终是一个测验).无论何时单击提交按钮,都应将选项突出显示为绿色表示正确,红色表示不正确.我在option.set上得到错误"Uncaught TypeError:undefined is not function"("highlight","green")|| 我的controllers/index.js文件中的option.set("highlight","red".当我在console.log(选项)时,我可以看到有一个属性突出显示的对象.我做错了什么?
路线/ index.js
var IndexRoute = Ember.Route.extend({
model: function() {
return Ember.A([
{
question: "What's up?",
answer: 'option b',
options: [
{
text: "option a",
active: false,
highlight: ''
},
{
text: "option b",
active: false,
highlight: '1'
}
]
},
{
question: "How many?",
answer: 'two',
options: [
"one",
"two"
]
}
]);
}
});
Run Code Online (Sandbox Code Playgroud)
控制器/ index.js
var IndexController = Ember.ObjectController.extend({
actions:{
submitAction : function(){
this.get('model').forEach(function (item){
item.options.forEach(function (option) {
if (option.text === item.answer) {
console.log(option);
option.set("highlight", "green");
console.log(option.highlight);
}
if (option.active && (option.text !== item.answer)) {
option.set("highlight", "red");
}
});
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
小智 19
object选项不是Ember对象,因此它没有get/ setmethods.
正如Krutius所说,您可以使用Ember.get()/ Ember.set()将属性设置为普通的旧JavaScript对象或Ember对象.例:
Ember.set(myObject, 'property', value);
var val = Ember.get(myObject, 'property');
Run Code Online (Sandbox Code Playgroud)
set:http://emberjs.com/api/#method_set
get:http://emberjs.com/api/#method_get