Ionic-AngularJS:通过Controller外部的模板调用方法 [英] Ionic - AngularJS: calling methods via template outside of Controller

查看:139
本文介绍了Ionic-AngularJS:通过Controller外部的模板调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这是示例代码:

<div ng-controller="MyControllerOne">
    <span ng-click="foobar()">Click Me!</span>
</div>

我可以在不更改控制器的情况下,从该模板中调用 MyControllerTwo 中的函数 foobar():

Can I, from that template, without changing controller, call the function foobar() in MyControllerTwo:

.controller('MyControllerOne', function($scope) {
    //some code
})
.controller('MyControllerTwo', function($scope) {
    // method I wanna call
    function foobar(){
    }
})

推荐答案

虽然不是最漂亮的解决方案,但在技术上还是有可能的.

While not the prettiest solution, it is technically possible...ish.

如果您将HTML更新为:

If you update your HTML to:

<div ng-controller="MyControllerOne">
    <span ng-controller="MyControllerTwo as mct" ng-click="mct.foobar()">Click Me!</span>
</div>

那您应该得到预期的结果.

Then you should get your expected results.

这篇关于Ionic-AngularJS:通过Controller外部的模板调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆