删除在拆分和连接angularjs中的字符串时不起作用
我在HTML中使用一个文本框和一个段落。当在文本框中留出空白时,使用拆分和联接概念在段落中添加下划线,但我的问题是在逐个删除文本框字母时,不会删除最后一个字母。这是因为拆分和连接的概念,通过删除拆分连接的概念,它的工作很好,但我需要拆分连接的概念以及删除删除在拆分和连接angularjs中的字符串时不起作用,angularjs,join,split,Angularjs,Join,Split,我在HTML中使用一个文本框和一个段落。当在文本框中留出空白时,使用拆分和联接概念在段落中添加下划线,但我的问题是在逐个删除文本框字母时,不会删除最后一个字母。这是因为拆分和连接的概念,通过删除拆分连接的概念,它的工作很好,但我需要拆分连接的概念以及删除 <div class="form-group"> <label for="name" class="control-label">Name</label> <input id="name
<div class="form-group">
<label for="name" class="control-label">Name</label>
<input id="nameid" ng-change="test()" ng-model="firstname" type="text" class="form-control" placeholder="Enter Name" required>
</div>
<p>{{editedname}}</p>
<script>
$scope.test= function (scope) {
var edtname= $scope.firstname.split(' ').join('_');
$scope.editedname = edtname;
};
</script>
名称
{{editedname}}
$scope.test=功能(范围){
var edtname=$scope.firstname.split(“”).join(“”);
$scope.editedname=edtname;
};
问题在于,由于输入字段上需要验证器,如果字段为空,modelfirstname
将变为未定义
,从而在未定义
上调用split时抛出错误
您只需检查字符串是否未定义
,如果是,则返回空字符串$scope.firstname | |'
:
angular.module('demo',[]).controller('mainCtrl',function($scope){
$scope.test=功能(范围){
var edtname=($scope.firstname||'').split('').join('');
$scope.editedname=edtname;
};
});代码>
名称
{{editedname}}
问题在于,由于输入字段上需要验证器,如果字段为空,modelfirstname
将变为未定义
,从而在未定义
上调用split时抛出错误
您只需检查字符串是否未定义
,如果是,则返回空字符串$scope.firstname | |'
:
angular.module('demo',[]).controller('mainCtrl',function($scope){
$scope.test=功能(范围){
var edtname=($scope.firstname||'').split('').join('');
$scope.editedname=edtname;
};
});代码>
名称
{{editedname}}
您的控制器在哪里???您好@Soosma谢谢您的快速回复,我的表格中有控制器,我在我的程序中提到了这一点,您是否需要它?我们希望您提供所有代码为什么在$scope.test function中通过参数?您的控制器在哪里???您好@Soosma谢谢您的快速回复,我的表单中有控制器,我在我的程序中提到了不在这里,你需要它吗?我们想要你的所有代码为什么你要在$scope.test函数中传递参数