绑定单选按钮和带有angularjs的输入文本字段

时间:2016-10-06 06:53:14

标签: javascript html angularjs binding

我对这段代码有些问题并且卡住了。我试图只在一行中显示两个问题的答案(来自两个问题),并且在#34;你的答案后,动态地在底部显示答案",如果用户在单选按钮或在单选按钮中更改选择,它们会动态更改下拉式菜单。到目前为止我只能显示一个答案。此外,选择输入文本字段中存在错误。当用户输入1时应该显示,2春天等等,但是现在它只适用于0到3而不是1到4的数字。我尝试了不同的尝试来解决这个问题但是没有成功



var app = angular.module('myApp', []);
            app.controller('myCtrl', function($scope) {
                $scope.actresses = [
                    {model : "Jenna"},
                    {model : "Mia"},
                    {model : "Julia"},
                    {model : "Olivia"}
                ];
                $scope.quarters=[
                    {model : "fall", number : 0},
                    {model : "spring", number : 1},
                    {model : "summer", number : 2},
                    {model : "winter", number : 3},
                ];
                
        $scope.change = function(a){
            console.log(a);
            $scope.selectedQuarter= $scope.quarters[a];
            console.log($scope.selectedQuarter);
        };
    });

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
        <section>
            <p><b>Question 1</b></p>
            <p>What is your favorite quarter?</p>
            
            <select ng-model="selectedQuarter" ng-options="x.model for x in quarters"></select>
            
            <input name='rad2' type="radio" ng-model="selectedQuarter" ng-value="quarters[0]"> fall
            <input name='rad2' type="radio" ng-model="selectedQuarter" ng-value="quarters[1]" > spring
            <input name='rad2' type="radio" ng-model="selectedQuarter" ng-value="quarters[2]"> summer
            <input name='rad2' type="radio" ng-model="selectedQuarter" ng-value="quarters[3]"> winter
            
            <p>Selection <input type="text" ng-model="selectedQuarter.number" ng-change="change(selectedQuarter.number)" /></p>
          </section>
        
        <section>
            <p><b>Question 2</b></p>
            <p>Who is your favorite actress?</p>
            
            <select ng-model="selectedActress" ng-options="x.model for x in actresses"></select>
            
            <input name='rad' type="radio" ng-model="selectedActress" ng-value="actresses[0]"> Jenna
            <input name='rad' type="radio" ng-model="selectedActress" ng-value="actresses[1]" > Mia
            <input name='rad' type="radio" ng-model="selectedActress" ng-value="actresses[2]"> Julia
            <input name='rad' type="radio" ng-model="selectedActress" ng-value="actresses[3]"> Olivia
        </section>
        
        <section>
            <p><b>your answers:</b></p>
           
            <div ng-switch="selectedActress.model">
                <div ng-switch-when="Jenna">
                    <p>Actress Jenna</p>
                </div>
                <div ng-switch-when="Mia">
                    <p>Actress Mia</p>
                </div>
                <div ng-switch-when=Julia>
                    <p>Actress Julia</p>
                </div>
                <div ng-switch-when=Olivia>
                    <p>Actress Olivia</p>
                </div>
            </div>
        </section>
&#13;
&#13;
&#13;  非常感谢你

1 个答案:

答案 0 :(得分:0)

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.actresses = [{
    model: "Jenna"
  }, {
    model: "Mia"
  }, {
    model: "Julia"
  }, {
    model: "Olivia"
  }];
  $scope.quarters = [{
    model: "fall",
    number: 0
  }, {
    model: "spring",
    number: 1
  }, {
    model: "summer",
    number: 2
  }, {
    model: "winter",
    number: 3
  }, ];

  $scope.change = function() {
    //console.log(a);
    $scope.selectedQuarter = $scope.quarters[$scope.number - 1];
    //console.log($scope.selectedQuarter);
  };
  $scope.$watch('selectedQuarter', function() {
    //console.log(a);
    if ($scope.selectedQuarter)
      $scope.number = $scope.selectedQuarter.number + 1;
    //console.log($scope.selectedQuarter);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <section>
    <p><b>Question 1</b>
    </p>
    <p>What is your favorite quarter?</p>

    <select ng-model="selectedQuarter" ng-options="x.model for x in quarters" ng-change="change1()"></select>

    <input name='rad2' type="radio" ng-model="selectedQuarter" ng-value="quarters[0]">fall
    <input name='rad2' type="radio" ng-model="selectedQuarter" ng-value="quarters[1]">spring
    <input name='rad2' type="radio" ng-model="selectedQuarter" ng-value="quarters[2]">summer
    <input name='rad2' type="radio" ng-model="selectedQuarter" ng-value="quarters[3]">winter

    <p>Selection
      <input type="number" ng-model="number" ng-change="change()" />
    </p>
  </section>

  <section>
    <p><b>Question 2</b>
    </p>
    <p>Who is your favorite actress?</p>

    <select ng-model="selectedActress" ng-options="x.model for x in actresses"></select>

    <input name='rad' type="radio" ng-model="selectedActress" ng-value="actresses[0]">Jenna
    <input name='rad' type="radio" ng-model="selectedActress" ng-value="actresses[1]">Mia
    <input name='rad' type="radio" ng-model="selectedActress" ng-value="actresses[2]">Julia
    <input name='rad' type="radio" ng-model="selectedActress" ng-value="actresses[3]">Olivia
  </section>

  <section>
    <p><b>your answers:</b>
    </p>

    <div ng-switch="selectedActress.model">
      <div ng-switch-when="Jenna">
        <p>Actress Jenna</p>
      </div>
      <div ng-switch-when="Mia">
        <p>Actress Mia</p>
      </div>
      <div ng-switch-when=Julia>
        <p>Actress Julia</p>
      </div>
      <div ng-switch-when=Olivia>
        <p>Actress Olivia</p>
      </div>
    </div>
  </section>