【问题标题】:$state.go() runs with no errors but doesn't actually work w/ Ionic$state.go() 运行没有错误,但实际上并不能使用 Ionic
【发布时间】:2015-08-22 12:27:09
【问题描述】:

我只是想在 ng-change 上运行一个函数后,从一系列选项中将状态更改为新模板。

一旦做出选择,我的函数运行良好,但在函数末尾调用的 $state.go() 实际上并没有将状态更改为我想要导航到的下一个状态视图。

以下是我的代码(简化)。我想在“template/pages/addit/index.html”中选择和选项,运行一个函数,然后将状态更改为“template/pages/addit/edit.html”。

app.js:

angular.module('starter', ['ionic','firebase'])

.config(function($stateProvider, $urlRouterProvider) {
 $stateProvider

.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/pages/menu/index.html",
controller: 'AppController'

.state('app.addit', {
url: "/addit",
views: {
  'menuContent': {
    templateUrl: "templates/pages/addit/index.html",
    controller: 'AddItController'
  }
}})

.state('app.addit.edit', {
url: "/edit",
views: {
    templateUrl: "edit.html",
    controller: 'EditItController'
}
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/addit');
});

add-it-controller.js:

angular.module('starter')
.controller('AddItController', function($scope, $state, addInventory){

$scope.selectNum = function (num) {
  addInventory(num) //works fine
  $state.go('addit.edit');//doesn't actually change the state
  console.log('the state is '+$state.current);

}
});

模板/页面/addit/index.html:

   <ion-view view-title="Addit">
  <ion-content>
  <div class="list card">

       <label class="item item-input item-select">
          <div class="input-label">
            How many?
          </div>
          <form >
            <select ng-change='selectNum(num)' ng-model="num"  required>

              <option  value='1'  selected>1</option>
              <option  value='2' >2</option>

            </select>
        </form>
      </label>
  </ion-content>
</ion-view>

【问题讨论】:

  • 换个网址有用吗?

标签: javascript angularjs ionic-framework


【解决方案1】:

可能 $state.go('app.addit.edit') 会在您的控制器中发挥作用。您错过了提供整个州名。

【讨论】:

  • console.log('状态为'+$state.current);在 $state.go() 语句之前和之后在控制台中打印什么?
  • 我认为问题在于嵌套视图和子视图的渲染。如果您可以重新检查不同 html 文件中的子视图,那就太好了。或者您可以创建一个 plnkr 以更好地理解该问题。 :)
【解决方案2】:

实际上,原来是 app.js 的问题,我应该有:

 .state('app.edit', {
    url: "/addit/edit",
    views: {
      'menuContent': {
        templateUrl: "templates/pages/addit/editphotos.html"
      }
    }
  });

我之前所做的是尝试为“编辑”页面创建状态,而不将其作为“菜单内容”的子状态

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 2017-03-26
    相关资源
    最近更新 更多