选择输入只使用angularjs选择一个值?

时间:2015-04-04 20:44:08

标签: javascript angularjs

到目前为止,我的代码将数据绑定到choices对象,然后在单击“添加课程”按钮时动态添加字段输入。然后,使用$http.get从服务器返回json数据并将其绑定到select选项。虽然当我选择一个选项时,它只会选择应用选项。

HTML:

<div ng-app="timeTable" ng-controller="addCoursesCtrl">
  <button ng-click="addNewCourse()">Add New Course</button>

  <fieldset ng-repeat="choice in choices">

    <select ng-model="choice.type" 
            ng-options="s.value as s.name for s in coursetoAdd">
      <option value="{{s.value}}">{{s.value}}</option>
    </select>

    <input ng-model="choice.course">

  </fieldset>
  <button ng-click="convertAndSend()">Submit</button>
</div>

使用Javascript:

var timeTable = angular.module("timeTable",[]);
timeTable.controller("addCoursesCtrl", function ($scope,$http) {
    $scope.choices = [{ course: '', type: '' }];
    $scope.coursetoAdd = $scope.choices;

    $http.get("/Semster/getSuggtedCourses").then(function (response) {
        $scope.coursetoAdd = response.data;
    });
    $scope.addNewCourse = function () {
        var newITemNo = $scope.choices.length + 1;
        $scope.choices.push({ course: '', type: '' });
    };
    $scope.convertAndSend = function () {
        var asJson = angular.toJson($scope.choices);
        console.log(asJson);
        $http.post('/Semster/Add', asJson);
    };

});

0 个答案:

没有答案