【问题标题】:getting an error " Uncaught SyntaxError: Unexpected token ("收到错误“ Uncaught SyntaxError: Unexpected token (”
【发布时间】:2017-04-06 22:28:31
【问题描述】:

在创建具有函数的工厂时,如果输入中从小写变为大写,则插入空格

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

【问题讨论】:

  • 输入:HelloWorld 输出:Hello World
  • 它工作正常。!!!见plnkr.co/edit/NZ7IlPojw0N2uWZY5bkv?p=preview
  • 不,它不能与工厂函数“processString”一起工作,没有工厂同样的函数工作
  • 请看上面的链接

标签: angularjs factory


【解决方案1】:

您的代码似乎正确!我刚刚为你做了一个sn-p,所以你可以检查一下。请检查项目其余部分中的代码。您在此处发布的代码是可以的。

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

【讨论】:

  • 谢谢,如果我在同一行返回后保留大括号,则发现问题,但如果我在下一行保持相同,我会收到错误?
  • 你找到了太好了 :)
猜你喜欢
  • 2018-02-17
  • 2015-04-21
  • 2014-04-07
  • 1970-01-01
  • 2012-05-17
  • 2019-02-10
  • 2014-07-08
  • 2011-03-09
  • 2014-01-06
相关资源
最近更新 更多