Search code examples
javascriptangularjsjsonaping-options

How to remove numbers in string from json using Angularjs?


i'm working on Angular. The Api in the code passes the json value with string which includes numbers and alphabets in it. But my requirement is to show only the alphabets and remove the numbers from the string in my dropdownlist .

My script :

staffService.getBaseBranches()
    .success(function(data) {
      $scope.baseBranches = data;

    })
    .error(function(error, status) {
      showError(error, status);
      notificationFactory.error("Unable to load base branches.");
    });

My html :

 <div class="form-group">
        <label>Branch : <i class="mandate">*</i></label>
           <select class="form-control input-md" name='branchName' ng-model="query.branchName" ng-options="branch.branchName as 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>

How can i do this ? Please help me from this .


Solution

  • You could change branchName while displaying it by calling showName function. showName will take care of removing digits from string

    ng-options="branch.branchName as showName(branch.branchName) for branch in baseBranches"
    

    Code

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