您可以使用sendAction()方法通過(guò)操作發(fā)送參數(shù),該方法為路由或控制器提供附加上下文以處理操作,并且將字符串'action'作為第一個(gè)參數(shù)。
this.sendAction('action', param1, param2);
<!DOCTYPE html> <html> <head> <title>Emberjs Sending Parameters with an Action</title> <!-- CDN's --> <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script> <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script> <script src="https://builds.emberjs.com/release/ember.debug.js"></script> <script src="https://builds.emberjs.com/beta/ember-data.js"></script> </head> <body> <script type="text/x-handlebars" data-template-name="index"> <p>{{my-comp title=title action="compFunc"}}</p> </script> <script type="text/x-handlebars" data-template-name="components/my-comp"> <input type="button" value="Click Me For Title" {{action "compFunc"}}/><br/> <p><b>Title:</b> {{title}}</p> </script> <script type="text/javascript"> App = Ember.Application.create(); App.MyCompComponent = Ember.Component.extend({ actions: { compFunc: function() { //simply addind an additional parameters to the sendAction() method this.sendAction('action', this.set('title', "Tutorialspoint")); } } }); </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上面的代碼保存在comp_param.html文件中
在瀏覽器中打開(kāi)此HTML文件。
更多建議: