最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJs ng-model substr - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Feb 1, 2016 at 21:17 IntoTheDeepIntoTheDeep 4,11815 gold badges45 silver badges86 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 12

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>

发布评论

评论列表(0)

  1. 暂无评论