收到错误“Uncaught SyntaxError:Unexpected token(”

时间:2017-04-06 14:14:38

标签: angularjs factory

创建一个带有函数的工厂,如果输入中的小写更改为大写,则插入空格

    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);
           };
        });

1 个答案:

答案 0 :(得分:0)

您的代码似乎正确!我刚给你做了一个片段,你可以查看一下。请检查项目其余部分的代码。你在这里发布的代码没问题。

&#13;
&#13;
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;
&#13;
&#13;