创建一个带有函数的工厂,如果输入中的小写更改为大写,则插入空格
var app = angular.module("myApp",[]);
app.factory('insertSpace',function()
{
return{
processString: function(input)
{
var output = "";
for(var i=0;i<input.length;i++)
{
if(i>0 && input[i] == input[i].toUpperCase())
{
output = output + " ";
}
output = output + input[i];
}
return output;
}
}
});
app.controller("myCtrl",function($scope,insertSpace)
{
$scope.convert = function(input)
{
$scope.output = insertSpace.processString(input);
};
});
答案 0 :(得分:0)
您的代码似乎正确!我刚给你做了一个片段,你可以查看一下。请检查项目其余部分的代码。你在这里发布的代码没问题。
var app = angular.module("myApp", []);
app.factory('insertSpace', function() {
return {
processString: function(input) {
var output = "";
for (var i = 0; i < input.length; i++) {
if (i > 0 && input[i] == input[i].toUpperCase()) {
output = output + " ";
}
output = output + input[i];
}
return output;
}
}
});
app.controller("myCtrl", function($scope, insertSpace) {
$scope.sampleInput = "HelloWorld";
$scope.convert = function(input) {
$scope.output = insertSpace.processString(input);
};
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="sampleInput" />
<br />
<br />
<a href="" ng-click="convert(sampleInput)">Convert</a>
<br />
<br />
<span ng-bind="output"></span>
</div>
&#13;