Javascript AngularJs ng-model substr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35140504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:18:47  来源:igfitidea点击:

AngularJs ng-model substr

javascripthtmlangularjs

提问by IntoTheDeep

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.

如何在没有 ng-model 的首字母的情况下设置输入值。我不想改变模型值本身。只是输入的内容没有首字母。

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

and js

和js

$scope.myVal = 'Hello';

Wanted result: ello

想要的结果:你好

回答by Dave Bush

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

假设您不想要两种方式的数据绑定,您可以使用 value 而不是 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.

如果您确实需要双向数据绑定,则在模型语句上使用 substr 没有意义。

回答by Urielzen

Have 2 models and use ng-change in your input to update the original model as you change your input model.

有 2 个模型并在输入中使用 ng-change 来在更改输入模型时更新原始模型。

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>