选择项目时,AngularJS选择框选项会消失

时间:2013-04-04 16:14:08

标签: javascript angularjs

我使用Angularjs创建了一个绑定到模型的选择框。 选择框选项正确加载,但只要选择任何一个选项,所有选项都会从选择框中消失。这是什么原因发生了,我如何防止我的选择消失?

Plunker链接演示了这个问题: http://plnkr.co/edit/DolBIN

HTML

<html xmlns="http://www.w3.org/1999/xhtml"  ng-app>
  <head>
      <title>Angular Test Prjoect - Home</title>
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
      <script type="text/javascript" src="Clinic.js"></script>
  </head>
  <body>
        <div ng-controller="ClinicCtrl">
          <select  ng-options="item as item.start + '-' + item.end + ':' + item.patient.name for item in appointments" ng-model="appointments">
          </select>
        </div>
  </body>
</html>

的JavaScript

function ClinicCtrl($scope) {
    $scope.appointments = [
        { start: "900", end: "930", provider: "1", patient: {name:"Allen",dob:"8/12/1977"} },
        { start: "1000", end: "1045", provider: "1", patient: { name: "Allen", dob: "8/12/1971"} },
        { start: "1030", end: "1100", provider: "2", patient: { name: "David", dob: "11/22/1973"} },
        { start: "1100", end: "1145", provider: "2", patient: { name: "Francine", dob: "3/18/1987"} },
        { start: "1230", end: "1530", provider: "3", patient: { name: "George", dob: "4/5/1997"} },
        { start: "1300", end: "1500", provider: "3", patient: { name: "Kirkman", dob: "6/28/1970"} }
    ];
}

1 个答案:

答案 0 :(得分:47)

问题是,只要选择了某个项目,select元素上的ng-model就会覆盖$scope.appointments数组。为ng-model值使用不同的范围属性。

这是一个更新的plunker:http://plnkr.co/edit/EAExby

模板的更改将是:

<div ng-controller="ClinicCtrl">
  <select
    ng-options="item as item.start + '-' + item.end + ':' + item.patient.name for item in appointments"
    ng-model="selectedAppointment"
  ></select>
  {{selectedAppointment}} <!-- log out the value just to show it's working -->
</div>