AngularJs ng-model substr

How to set the input value without the first letter of the ng-model. I do not want to change the model itself. Just input content without first letter.

<input type="text" ng-model="myVal" >

and js

$scope.myVal = 'Hello';

Required Result: ello

+4
source share
2 answers

Assuming you don't want to use two-way data-bindin, you can use a value instead of ng-model

<input type="text" value="{{myVal.substr(1)}}" >

If you want two-way data binding, using substr in the model description does not make sense.

+3
source

You have 2 models and use ng-change in your input to update the original model when changing your input model.

angular.module('myApp',[]).filter('myFilter',function(){
  return function(input){
    input = input.substring(1,input.length);
    return input;
    };
  })
.controller('myController',function($scope,$filter){
  $scope.myValObj = "Hello";
  $scope.myValObj2 = $filter('myFilter')($scope.myValObj);
  $scope.updateOriginal = function(){
    $scope.myValObj = $scope.myValObj[0] + $scope.myValObj2;
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
  <input type="text" ng-model="myValObj2" ng-change="updateOriginal()"/>
<br>
  Original Model: {{myValObj}}
<br>
  Model For Input: {{myValObj2}}
</div>
Run codeHide result
+2
source

Source: https://habr.com/ru/post/1626922/


All Articles