【问题标题】:AngularJS - Navigate to another view via $state.goAngularJS - 通过 $state.go 导航到另一个视图
【发布时间】:2015-12-02 22:20:23
【问题描述】:

我正在使用 Ionic 框架为移动设备创建单页应用程序。我的个人资料页面有一个基于选项卡的结构。在“主页”视图中,我有一个链接“编辑配置文件”,一旦单击它应该通过 ProfileManagementController.js 导航到 editprofile.html。当我单击它时,没有任何反应,控制台中也没有显示错误。只有地址栏中的 url 被更新。

这里是 app.js 中的路由代码:

.state('editProfile', {
    url: '/editProfile',
    views: {
      'tab-editprofile': {
        templateUrl: 'templates/profile/editprofile.html',
        controller: 'ProfileManagementController'
      }
    }
  })

这里是与此功能相关的 ProfileManagementController.js 剪辑器:

$scope.goToEditProfile = function(path)
{
    $state.go(path);
}

这是主页选项卡视图 (tab-home.html) 的 HTMl 表单,其中存在用于导航到编辑配置文件的按钮:

<div>
          Name: {{tempUserObject.name}} <br/>
          Friends: {{tempUserObject.totalFriends}}<br/>
          Mobile: {{tempUserObject.mobile}}<br/>
          Username: {{tempUserObject.username}} <br/>  
          <br/>
            <button ng-click="goToEditProfile('editProfile')" class="button">Edit Profile</button>

        </div>

这是我第一次登陆 tab-home.html 并单击不起作用的按钮后的 URL:

之前:http://127.0.0.1:49259/index.html#/tab/home 之后:http://127.0.0.1:49259/index.html#/editProfile

请帮助指导我找出此链接出了什么问题。我之前在其他地方使用过 $state.go 并且效果很好。

更新:添加了editprofile.html。

<ion-view hide-nav-bar="false" title="Edit Profile">
    <ion-content padding="'true'" class="has-header">
        <div class="spacer" style="width: 300px; height: 87px;"></div>
        <div class="button-bar"></div>
        <h2>Edit Profile</h2>
    </ion-content>
</ion-view>

【问题讨论】:

  • 将其更改为相对路径是否有效?即ng-click="goToEditProfile('^.editProfile')"
  • @potatopeelings 已经尝试过了,但没有

标签: angularjs ionic-framework


【解决方案1】:

使用它而不是状态更改。

 $location.path('/editProfile');

别忘了在你的控制器函数中注入 $location。

【讨论】:

  • 是的,现在这样做并注入了位置,但没有用。只有 URL 更改为如前所述。 $state 不是问题,所以 $location 不是解决方案。同一 app.js 中的其他函数与状态一起使用
  • 你用的是ui-router吗!!如果是,请尝试ui-sref ="editProfile" 也通过此stackoverflow.com/questions/21023763/…
  • 不,我没有。只使用基础 AngularJS 和 Ionic Framework 自带的
  • 看来 ngRoute 对 Ionic 框架有很强的依赖关系,我没有发现你的代码有什么问题,它可能也依赖于 angular 或 ionic 框架的版本,在迭代版本中可能无法始终如一地工作.事实上 ngRoute 不再是一个核心包。特别是,您使用 ionic 他们推荐 ui-router。查看他们的博客。 ionicframework.com/docs/angularjs/controllers/view-state
  • 也通过这个来解决问题forum.ionicframework.com/t/…
【解决方案2】:

您的 app.js 中是否定义了一个名为“路径”的状态? 如果不使用模板和控制器创建一个名为“path”的状态,就像“editprofile”状态一样

【讨论】:

    【解决方案3】:

    所以我通过将 app.js 中的路由修改为以下内容解决了上述问题:

     .state('editprofile', {
        url: '/editprofile',
        templateUrl: 'templates/profile/editprofile.html',
        controller: 'ProfileManagementController'
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多