如何在 Angular.js 中使用 ng-model?

How to use ng-model in Angular.js?

我是 Angular.js 的新手。问题出在我的 ng-model 和 ng-option 上。我有一个数据,它在我的下拉列表中显示代码编号和城市名称。我做了一些更改以仅显示城市并删除下拉列表中的数字,它工作正常 Ex: CHENNAI。但是,如果我使用 ng-model 并在我的 Angular 表达式中显示值,我仍然得到相同的数据 EX: 0123 CHENNAI。使用 ng-model 如何在 Angular 表达式中仅显示城市名称,即 {{}}.

这里是 html:

 <div class="form-group">
    <label>Branch : <i class="mandate">*</i></label>
      <select class="form-control input-md" name='branchName' ng-model="query.branch.branchName" ng-options="branch.branchName as   showName(branch.branchName) for branch in baseBranches">
        <option value="" selected>-- Select Branch --</option>
      </select>
   <span class="error" ng-show="search_form.branchName.$error.required">Branch  is  required</span>
 </div>

以及删除数字的脚本:

$scope.showName = function(branchName){
   return branchName.replace(/\d+/g, '')
   //alert(branchName);
}

请帮忙解决这个问题,因为我是 Angularjs 的新手。

将您的数据更改为

$scope.array =[{
 number: 123,
 name: "abc"
},
{
 number: 123,
 name: "abc"
}]

如果您希望显示的模型值和文本值都没有数字,即“123CHENNAI”,因为 "CHENNAI" 使用您为模型值编写的相同 "showName" 函数如下所示:

 <select class="form-control input-md" name='branchName' ng-model="query.branch.branchName" ng-options="showName(branch.branchName) as   showName(branch.branchName) for branch in baseBranches">
   <option value="" selected>-- Select Branch --</option>
 </select>

Fiddle link here,请检查这是否是您所期望的。