Search for the blank angularjs drop-down box, and many solutions can appear. However, for static fields, there is currently no solution found online, as follows:
1 <select class="form-control" ng-model="UserState"2 ng-init="UserState=0">3 <option value="-1">选择状态</option>4 <option value="0">在职</option>5 <option value="1">离职</option>6 </select>
If you want to follow the online To solve the problem, you first need to store the drop-down options in an object array. It's okay for one or two drop-down boxes. If there are many such drop-down boxes in the project, it will take a lot of effort. The simplest and crudest method is to modify the angularjs source code so that whitespace characters are no longer generated. However, my ability is limited and I couldn't find the place to add blank lines. However, it is gratifying to find that the blank characters appear because of the use of ng-model. Simply write an instruction to replace the function of ng-model, which can also solve the problem of drop-down blanks. problem, so I wrote an instruction:
app.directive("dModel",function () { 2 return { 3 restrict:'A', 4 compile:function(element, attrs, transclude){ 5 console.log (transclude); 6 var dmodel = Attrs ["dmodel"]; 7 Return {8 Pre: Function (scope, ielement, ictrs) {9 10 VAR SelectValue = scope [dmodel]+"" "; 11 if (selectvalue 12 options[i ].setAttribute("selected",true);19 break; }22 ("change",function () {26 var selectValue=this.value;27 scope.$apply(function () {28 scope[dModel]= selectValue;29 );30
用该指令替代ng-model即可实现相同的功能,同时不会出现下拉空白,当然,这个指令毕竟是自己随便写的几行代码,远远不如ng-model强大,比如动态修改指令绑定的值来改变选中项还不能实现,仅仅可以在第一次初始化时绑定选中项,另外也不支持ng-selected。如果读者有兴趣,可以自己实现后续的功能,对于我来说,已经可以满足需要了。
The above is the detailed content of Angularjs drop-down box blank solution. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn