![](/img/trans.png)
[英]Can we access the respective controller of the view inside the init hook of the view
[英]How to access a variable in the template inside its' respective controller?
我正在尝试在Ember中创建某种搜索功能,但在将输入字段的值传递给控制器进行处理时遇到了一些麻烦。
这就是我所拥有的
App.ApplicationController = Ember.Controller.extend({
actions: {
handle_search: function() {
var search_text = this.get('controllers.search_text');
console.log(search_text);
}
}
});
这就是我在模板中的内容
<span class="input-group-addon"><i class="fa fa-search"></i></span>
{{input type="text" valueBinding=search_text class="form-control" placeholder="Search..."}}
<span class="input-group-btn">
<button {{action 'handle_search'}} class="btn btn-default" type="button">Search</button>
</span>
这给我以下错误
WARNING: You're attempting to render a view by passing valueBinding=search_text to a view helper, but this syntax is ambiguous. You should either surround search_text in quotes or remove `Binding` from valueBinding. ember.js:3521
Error Uncaught object
所以我也尝试使用value
而不是valueBinding
和this.controller.get('search_text') instead of
不是valueBinding
('controllers.search_text');`,但出现此错误
Uncaught TypeError: Cannot read property 'get' of undefined app.js:127
那么,如何访问模板中设置的控制器中的值? search_text
变量只是我在模板中创建的名称,因此我可以尝试在控制器上访问输入的值。
这些属性直接存在于控制器上,因此可以使用this.get('foo')
App.ApplicationController = Ember.Controller.extend({
actions: {
handle_search: function() {
var search_text = this.get('search_text');
console.log(search_text);
}
}
});
而且,仅当您使用名称的文本版本绑定到属性时,才应使用valueBinding='foo'
,例如valueBinding='foo'
。 如果要直接绑定到属性,则只需执行value=foo
。
{{input type="text" value=search_text class="form-control" placeholder="Search..."}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.