Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs ng-model substr

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

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

and js

$scope.myVal = 'Hello';

Wanted result: ello

like image 633
IntoTheDeep Avatar asked Feb 01 '16 21:02

IntoTheDeep


2 Answers

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

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

If you do want two way databinding, using substr on a model statement doesn't make sense.

like image 82
Dave Bush Avatar answered Oct 29 '22 15:10

Dave Bush


Have 2 models and use ng-change in your input to update the original model as you change 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>
like image 25
Urielzen Avatar answered Oct 29 '22 13:10

Urielzen