简体   繁体   中英

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

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.

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM