【问题标题】:move to different pages based on the checked radio button根据选中的单选按钮移动到不同的页面
【发布时间】:2014-12-15 11:37:21
【问题描述】:

我有这个表单,它使用了 3 个离子单选按钮。有没有办法根据选择的单选按钮移动到不同的页面。例如:我的单选按钮被命名

1.USO 2.OASO 3.TFTSO

如果我选择 USO,我想移动到另一个页面,如果我选择 OASO,我想移动到另一个不同的页面等等。我只想在单击我在代码中实现的“继续”按钮后移动。

这就是我在 html 中通过单选按钮和继续按钮实现的方式

<label class="labelColor">
     <h5><b>Select Standing Order Type</b></h5>
</label>
<div class="list">
     <ion-radio ng-repeat="item in clientSideList"
                ng-value="item.value"
                ng-model="data.clientSide">
      {{ item.text }}
      </ion-radio>       
</div>

 <!-- BUTTONS -->
 <div class="col" 
      style="text-align: center">
      <button align="left" 
              class="button button-block button-reset" 
              style="display: 
              inline-block;width:100px;text-align:center " 
              type="reset"
              ng-click="submitted = false;  reset()" 
              padding-top="true">
       Reset
      </button>
      <button class="button button-block button-positive"  
              style="display: inline-block;width:100px "
              ng-click="submitted=true; "padding-top="true">
      Proceed
      </button>
</div>

我的 angularjs

.controller('OrderCtrl', function($scope,$state) {
     $scope.clientSideList = [
          { text: "Utility Standing Order", value: "uso" },
          { text: "Own Account Standing Order", value: "oaso" },
          { text: "Third Party Fund Transfer Standing Order", value: "tpftso" }
     ];

     $scope.data = {
       clientSide: 'uso'
     };

})

我的控制器

.controller('OrderCtrl', function($scope,$state, $http,  $ionicPopup) {





     $scope.clientSideList = [
    { text: "Utility Standing Order", value: "uso"  },
    { text: "Own Account Standing Order", value: "oaso"  },
    { text: "Third Party Fund Transfer Standing Order", value: "tpftso" }

  ];



  $scope.data = {
    clientSide: 'uso'
  };

    $scope.move = function () {
    var path;
    switch($scope.data.clientSide) {
      case 'uso': path = '/so/utilitystanding'; break;
      case 'oaso': path = '/so/ownstandingorder'; break;
      case 'tpftso': path = '/so/thirdstandingorder'; break;
    }
    $state.go(app.standing);
  };


            })



.controller('utilityStandingCtrl', function($scope,$state) {

});

【问题讨论】:

  • 获取单选按钮的 id 并检查它是否为 checker 。如果选中,则使用 location.href='the specific url page'
  • 你能给我一个例子吗:)
  • 为什么不能只使用更改处理程序? ng-change
  • 我想要的是当按下我的继续按钮时,我的表单应该根据选择的单选按钮移动到不同的页面。使用 ng-change,我该如何实现?

标签: javascript jquery html angularjs ionic-framework


【解决方案1】:

尝试将ng-click 添加到您的Proceed 按钮并定义函数,该函数将分析您选择的data.clientSide 的值,然后调用$location.path(url)。您需要将$location 服务注入您的控制器。
JavaScript:

  $scope.goSomewhere = function () {
    var path;
    switch($scope.data.clientSide) {
      case 'uso': path = '/uso'; break;
      case 'oaso': path = '/oaso'; break;
      case 'tpftso': path = '/tpftso'; break;
    }
    $location.path(path);
  };

还有 HTML:

<button ng-click="goSomewhere()">Proceed</button>

演示:http://plnkr.co/edit/jawx0OivVmu2EyNmAbR9?p=preview

编辑

我上面的回答与angular-route有关,与ui-router无关。最后,您需要使用除函数调用之外的所有相同代码。而不是location.path(),它必须是我

$state.go(yourStateName)

请注意,不是url,而是州名。

编辑2

这是ionic框架的代码(几乎相同)+应用程序的设置。

配置:

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

    .state('main', {
      url: "/main",
      templateUrl: 'main.html'
    })
      .state('usoStateName', {
        url: '/uso',
        templateUrl: 'page.html',
        controller: 'usoCtrl'
      })
      .state('oasoStateName', {
        url: '/oaso',
        templateUrl: 'page.html',
        controller: 'oasoCtrl'
      })
      .state('tpftsoStateName', {
        url: '/tpftso',
        templateUrl: 'page.html',
        controller: 'tpftsoCtrl'
      });
      $urlRouterProvider.otherwise('/main');
  });

及功能:

  $scope.goSomewhere = function () {
    var path;
    switch($scope.data.clientSide) {
      case 'uso': path = 'usoStateName'; break;
      case 'oaso': path = 'oasoStateName'; break;
      case 'tpftso': path = 'tpftsoStateName'; break;
    }
    $state.go(path);
  };

演示:http://plnkr.co/edit/0sk3dECPNm35HgMqiprt?p=preview

【讨论】:

  • 我应该给什么路径?我给了我的 uso 页面的路径,但什么也没发生
  • @CraZyDroiD:你的路径怎么样?你在哪里尝试过?
  • @CraZyDroiD:对不起,我的错。我只是没有看到它是ionic 框架。请参阅我的更新以获取答案。
  • 我更新了我的答案。我包括了我的控制器。你能看看并告诉我我做错了什么
  • @CraZyDroiD:app.standing 是什么?您需要使用path 变量,例如:$state.go(path)。但似乎它也将是错误的(我不确定)。如果这不起作用 - 您可以提供模块的设置(.config() 函数 - 您设置状态的位置) - 那么我可以帮助您。
【解决方案2】:

而不是 {{ item.text }}

试试这样的:

a href=""#/viewName/{{item.text }}"}">{{ item.text }}

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    相关资源
    最近更新 更多