当我通过以下方式找到 DOM 元素时:
$('md-autocomplete-wrap button')[0]
浏览器控制台实际上正确显示为 HTML 按钮,我想通过 Controller 操作单击此按钮。 尽管如此:
$('md-autocomplete-wrap button')[0].click();
没有成功,因为什么也没有发生。如何正确触发该按钮的click()
函数?
最佳答案
使用此代码:
http://emberjs.jsbin.com/mimuwale/1/edit
js
App = Ember.Application.create();
function getView($el){
return Ember.View.views[$el.closest(".ember-view").attr("id")];
}
function newAlert(el){
getView($(el)).get('controller').send('newAlert',el.id);
}
App.IndexView=Ember.View.extend();
App.IndexController=Ember.Controller.extend({
actions:{
newAlert:function(buttonId){alert('clicked button:'+buttonId);}
}
});
hbs
<script type="text/x-handlebars" data-template-name="index">
<button id='definition' onclick='newAlert(this)'>test</button>
</script>
关于javascript - 如何从 ember.js Controller 单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45534015/