javascript - 如何设置在 AngularJs 中最初选中的单选按钮?

标签 javascript html angularjs

我创建了两个选项卡,其中选项卡通过选中单选按钮进行导航。这两个选项卡之一最初将处于事件状态,但我需要首先选中相应的单选按钮。

<div ng-app="myApp">
  <div class="account-tab-selector" id="tabs" ng-controller="TabsCtrl">
    <ul>
      <li class="col-sm-6 text-center" ng-repeat="tab in tabs">
        <label for="{{tab.url}}">
          <input ng-class="{active:isActiveTab(tab.url)}" ng-click="onClickTab(tab)" type="radio" name="radio" id="{{tab.url}}" ng-model="choice.isSelected" value="true">{{tab.title}}</label>
      </li>
    </ul>
    <div class="clearfix"></div>
    <div id="mainView">
      <div ng-include="currentTab"></div>
    </div>
  </div>
  <script type="text/ng-template" id="one.tpl.html">
    <div class="customer-reg-form" id="new_customer_form">
      <div class="sign-up-fb">
        <a href="#"><i class="fa fa-facebook" aria-hidden="true"></i>
                                    Sign Up With Facebook</a>
      </div>
      <div class="reg-form">
        <div class="or-separator"><span>OR</span></div>
        <div class="row">
          <div class="col-sm-6 form-group">
            <label for="fname">First Name<span>*</span></label>
            <div class="input-group">
              <input name="firstname" id="fname">
            </div>
          </div>
          <div class="col-sm-6 form-group">
            <label for="lname">Last Name<span>*</span></label>
            <div class="input-group">
              <input name="lastname" id="lname">
            </div>
          </div>
          <div class="col-sm-6 form-group">
            <label for="mnumber">Mobile Number<span>*</span></label>
            <div class="input-group">
              <input name="mobile-number" id="mnumber">
            </div>
          </div>
          <div class="col-sm-6 form-group">
            <label for="dlocation">Default Location<span>*</span></label>
            <div class="input-group">
              <select name="default-location" class="dropstyle">
                <option></option>
                <option>Kansas</option>
              </select>
            </div>
          </div>
          <div class="col-sm-12 form-group">
            <label for="email">Email Address<span>*</span></label>
            <div class="input-group">
              <input name="email" id="email">
            </div>
          </div>
          <div class="col-sm-12 form-group">
            <label for="password">Password<span>*</span></label>
            <div class="input-group">
              <input name="password" id="password">
            </div>
          </div>
        </div>
        <div class="agreement">By clicking "Register" below, you are agreeing to our <a href="#">Terms of Service</a> agreement.</div>
      </div>
    </div>
  </script>
  <script type="text/ng-template" id="two.tpl.html">
    <div class="customer-reg-form" id="existing_customer_form">
      <div class="sign-up-fb">
        <a href="#"><i class="fa fa-facebook" aria-hidden="true"></i>
                                    Login With Facebook</a>
      </div>
      <div class="reg-form">
        <div class="or-separator"><span>OR</span></div>
        <div class="row">
          <div class="col-sm-12">
            <label for="mnumber">Email Address<span>*</span></label>
            <input id="mnumber">
          </div>
          <div class="col-sm-12">
            <label for="mnumber">Password<span>*</span></label>
            <input id="mnumber">
          </div>
        </div>
        <div class="forgot-pwd"><a href="#">Forgot Password?</a></div>
      </div>
    </div>
  </script>
</div>


var app = angular.module("myApp", []);
app.controller('TabsCtrl', ['$scope', function($scope) {
  $scope.tabs = [{
    title: 'I am a new customer',
    url: 'one.tpl.html',
    isSelected: true
  }, {
    title: 'I have an account',
    url: 'two.tpl.html'
  }];
  $scope.currentTab = 'one.tpl.html';

  $scope.onClickTab = function(tab) {
    $scope.currentTab = tab.url;
  };

  $scope.isActiveTab = function(tabUrl) {
    return tabUrl == $scope.currentTab;
  };
}]);

Jsfiddle here

最佳答案

您已将单选按钮与 $scope.choice.isSelected 绑定(bind),您可以在 Controller 中或在 ng-init 中为其设置值。 你能在你的 Controller 中添加以下两行吗?

$scope.choice={};
$scope.choice.isSelected = "true";

或者以更好的方式,您可以像这样修改标签:

<li class="col-sm-6 text-center" ng-repeat="tab in tabs">
        <label for="{{tab.url}}">
          <input ng-class="{active:isActiveTab(tab.url)}" ng-click="onClickTab(tab)" type="radio" name="radio" id="{{tab.url}}" ng-model="tab.isRadioChecked" value="true">{{tab.title}}</label>
      </li>

在你的 Controller 中。

$scope.tabs = [{
    title: 'I am a new customer',
    url: 'one.tpl.html',
    isSelected: true,
isRadioChecked: "true"
  }, {
    title: 'I have an account',
    url: 'two.tpl.html'
  }];

关于javascript - 如何设置在 AngularJs 中最初选中的单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39098631/

相关文章:

javascript - 如何在 ng-repeat 循环中分配一个 angularjs 事件监听器?

javascript - jQuery:如何从给定的标记生成进度条

javascript - 如何在flask中处理请求json数据

jquery - 如何使用 JQuery 在输入类型文件中验证文件扩展名?

javascript - HTML 到 PHP 中数组内部的数组

javascript - AngularJS - 在路由和模板中评估 cookie

javascript - 未调用地理编码反向回调函数

JavaScript - 保存、存储和更新数组

javascript - jQuery 鼠标悬停函数在 $(window).resize 之后仍然触发

AngularJS 选择创建值为 "? object:null ?"的选项,并且在绑定(bind)到 null 时不使用空的默认选项