gpt4 book ai didi

javascript - 如果下拉值的第一个字符是空格,如何删除它? Angular.js

转载 作者:行者123 更新时间:2023-12-03 05:21:25 28 4
gpt4 key购买 nike

我有相当广泛的逻辑。这只是一个例子。这就是为什么我需要从 View 中做一些事情。您可以给我什么想法来做到这一点,或者更简单的方法不从 Controller 中做到这一点。

<select class="form-control" ng-model='zoo' id='zoo' name='animal'  
ng-options="item as item.animal for item in animals track by item.animal">
</select>

我的想法或多或少

<select class="form-control" ng-model='zoo' id='contrato_objetivo' name='animal'  
ng-options="item as item.animal.charAt(0)==" "?item.animal.substr(1):item.animal for item in animals track by item.animal">

在 View 中,如果第一个字符有空格,如何删除?

http://fiddle.jshell.net/3g2pmrom/

谢谢!

最佳答案

如果您可以添加一个过滤器,这意味着您不会触摸 Controller ,这将会有所帮助。

angular.module("myApp", ['ui.bootstrap'])
.controller("MyCtrl", function($scope, $modal) {
$scope.obj={ };
$scope.animals=[{"animal":" cat"},{"animal":"dog"}];


}).filter('removespace', function () {
return function (val) {
return (!val) ? '' : val.replace(/ /g, '');
};
});

html

<div ng-app="myApp">

<div ng-controller="MyCtrl">
<select class="form-control" ng-model='zoo' id='contrato_objetivo' name='animal' ng-options="item as item.animal for item in animals track by item.animal | removespace">
<option style="display:none" value="">Select</option>
</select>
</div>

fiddle :http://fiddle.jshell.net/eaknm1yh/

该选项的值将不带空格。

关于javascript - 如果下拉值的第一个字符是空格,如何删除它? Angular.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41364764/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com