javascript - 如何使用 ngroute 和单选按钮来更改 View

标签 javascript html angularjs ngroute

现在我已经编写了一段代码来使用 ng-route 更改页面的 View 。代码是

<a href="#cod">Cash on delivery</a>
<a href="#online">Online Payment</a>
<ng-view>
</ng-view>

js代码是

myApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/cod', {
        templateUrl: './cod.html',
        controller:'codController'
    }).
      when('/online', {
        templateUrl: './online.html',
        controller:'onlineController'
      }).
      otherwise({
        redirectTo: '/cartdisplay.php'
      });
}]);

工作正常, View 正在正确更改。 但我需要单选按钮来更改 View ,例如

 <a href="#cod">
     <input type="radio" name="pay" id="cod">
     <label for="cod">COD</label>
 </a>
 <a href="#online">
     <input type="radio" name="pay" id="online">
     <label for="online">Online Payment</label>
 </a>
 <ng-view>
 </ng-view>

但现在观点没有改变。 ngRoute 不起作用, 有什么办法可以解决这个问题。

最佳答案

<input type="radio" name="pay" id="cod" ng-model="someVariableOfScope"><label for="cod" data-ng-click="changeRoute('cod')">COD</label>
    <input type="radio" name="pay" id="online" data-ng-click="changeRoute('online')"><label for="online" ng-model="someVariableOfScope ">Online Payment</label>
             <ng-view>
         </ng-view>

在你的 Controller 中:

    function SomeCommonController($scope, $location, $rootScope) {
     $scope.changeRoute = function(routeKey) {
       if ( $scope.routeKey == 'cod' ) { // test

            $location.path( "/cod" );
        } else ( $scope.routeKey == 'online' ) { {
                    $location.path( "/online" );
        }
    }

关于javascript - 如何使用 ngroute 和单选按钮来更改 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36082855/

相关文章:

html - 为什么为两个内联 block 兄弟 div 之一设置行高会影响两个 div?

html - 水平展开div不溢出 :hidden

javascript - 使用 javascript 计算字数 - Postman 测试

javascript - $(this).val() 不返回值标签的值

python - 我的 css 和图像没有在 django 中显示

javascript - 创建一个类似于 POST 请求的链接

angularjs - mdInput 消息显示在无效消息元素 : 上调用的动画

javascript - 使用 Angular 登录后获取 Facebook 详细信息

c# - 如何从javascript调用服务器端函数

javascript - 只打印 <div id ="printarea"></div>?