angularjs - 每当打开/显示 View 时运行 Controller 函数

标签 angularjs ionic-framework

我正在构建一个使用 Angular+ionic 的应用程序,该应用程序在底部使用经典的三按钮菜单,其中包含三个 ionic 选项卡。当用户单击选项卡时,该模板将通过 ui-router 打开。

我有这样的状态:

$stateProvider
  .state('other', {
    url: "/other",
    abstract: true,
    templateUrl: "templates/other/other.html"
})

在模板中我做了类似的事情:

<ion-nav-view name="other" ng-init="doSomething()"></ion-nav-view>

我知道我可以在 Controller 中编写 doSomething() 函数,然后在那里手动调用它。但这给了我同样的问题。我似乎无法弄清楚如何在有人打开该 View 时多次调用 doSomething() 函数。

现在,doSomething() 函数被调用得很好,但只是在用户第一次打开 View /选项卡时。每当用户打开该 View 或选项卡时,我想调用一个函数(以更新地理位置)。

实现这一点的正确方法是什么?

最佳答案

默认情况下,您的 Controller 是缓存的,这就是您的 Controller 仅触发一次的原因。要关闭某个 Controller 的缓存,您必须修改 .config(..).state 并将 cache 选项设置为 false。例如:

  .state('myApp', {
    cache: false,
    url: "/form",
    views: {
      'menuContent': {
        templateUrl: "templates/form.html",
        controller: 'formCtrl'
      }
    }
  })

如需进一步阅读,请访问http://ionicframework.com/docs/api/directive/ionNavView/

关于angularjs - 每当打开/显示 View 时运行 Controller 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27058276/

相关文章:

android - Ionic click/tap 事件不会触发外部 URL

html - 如何防止元素文本换行

javascript - 空数组中的 Angular 推送项

javascript - Yeoman - 当前使用 AngularJS 和 Express 的最佳方式?

javascript - ionic : ReferenceError: $ is not defined

javascript - 如何发起一个简单的 CORS 请求?

javascript - 当我传递一个 ID 时,函数参数接收到一个空字符串

javascript - Angular Material 中具有相同内容的选项卡

javascript - Angular ng 消息 : how to check password confirmation?

javascript - ng-model 值没有通过 ng-repeat 传递