简体   繁体   English

ng-change 正在清除预先选择的下拉选项

[英]ng-change is wiping out the pre-selected drop down option

I have been implementing some live searching using ng-change and it is working very well apart from this issue I have with a pre-selected drop down box.我一直在使用 ng-change 实施一些实时搜索,除了我使用预选下拉框遇到的这个问题外,它运行得非常好。

When I serve up the page, I set the selected="selected" I want to be the default option.当我提供页面时,我将selected="selected"设置为默认选项。 However, when the page loads, it just jumps to the top option.但是,当页面加载时,它只是跳转到顶部选项。 I need the drop down box to stay on option three.我需要下拉框保持选项三。

<html>
<body >

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js" type="text/javascript"></script>

<script type="text/javascript">
  angular.module('myApp', [])
       .controller('Ctrl', function ($scope, $http)
       {
       });
</script>

<form name="aspnetForm" method="post" action="./Test2.aspx" id="aspnetForm">

  <div ng-app="myApp" ng-controller="Ctrl">
    <select name="DropDownTest" id="DropDownTest" ng-model="LiveSearchOptions" ng-change="SearchFieldChanged()">
      <option value="">ALL</option>
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3" selected="selected">Three</option>
      <option value="4">Four</option>
      <option value="5">Five</option>
    </select>
  </div>

</form>

When I remove the module (by just deleting the script tags) it works ok.当我删除模块时(通过删除脚本标签)它工作正常。 Can you advise please?你能建议吗? I am quite new to this!我对此很陌生!

JS Fiddle to play about with https://jsfiddle.net/4to3ux4g/14/ JS Fiddle 与https://jsfiddle.net/4to3ux4g/14/一起玩

You have attached an ng-model to the controller.您已将ng-model附加到控制器。 So the selected attribute is getting overridden by value in your ng-model因此selected属性被ng-model值覆盖

 angular.module('myApp', [])
  .controller('Ctrl', function ($scope, $http)
  {
     $scope.LiveSearchOptions = '3';
  });

Fiddle: https://jsfiddle.net/BoyWithSilverWings/bbsymmr8/1/小提琴: https : //jsfiddle.net/BoyWithSilverWings/bbsymmr8/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM