【问题标题】:Angular syntax error角度语法错误
【发布时间】:2017-09-27 17:54:38
【问题描述】:

Angular 新手。在找出问题所在时遇到问题。我看到有语法错误,但我不知道怎么做?请指教,谢谢。 这是我的错误:

Error: [$parse:syntax] http://errors.angularjs.org/1.6.4/$parse/syntax?p0=Express&p1=is%20unexpected%2C%20expecting%20%5B%7D%5D&p2=47&p3=prefixes%3D%7BVisa%3A%20'4'%2C%20MasterCard%3A'5'%2C%20American%20Express%3A'3'%2C%20Discover%3A'6'%7D&p4=Express%3A'3'%2C%20Discover%3A'6'%7D

 <div class="ng-scope" ng-app="edu_app" ng-controller="edu_ctrl" ng-init="prefixes={Visa: '4', MasterCard:'5', American Express:'3', Discover:'6'}" style="margin-bottom:25px;">

这里是 HTML:

<div ng-app="edu_app" ng-controller="edu_ctrl" ng-init="prefixes={Visa: '4', MasterCard:'5', American Express:'3', Discover:'6'}">
    <div>
        <div>
            <img id="visa" src="images/visa-card-logo.png" ng-class="{'greyed' : prefixes.type !== 'Visa'}" />
        </div>
        <div>
            <img id="mastercard" src="images/master-card-logo.png" ng-class="{'greyed' : prefixes.type !== 'MasterCard'}"  />
        </div>
        <div>
            <img id="amex" src="images/amex-card-logo.png" ng-class="{'greyed' : prefixes.type !== 'American Express'}"  />
        </div>
        <div>
            <img id="discover" src="images/discover-card-logo.png" ng-class="{'greyed' : prefixes.type !== 'Discover'}"  />
        </div>
    </div>
</div>

Javascript:

angular.module('edu_app',[]).controller('edu_ctrl', function($scope) {
        $scope.prefixes = {};
        $scope.ccNumberChnage = function() {
            var ccType = $scope.prefixes.cards ? $scope.prefixes.cards.charAt(0) : '';
            switch(ccType) {
                case '3': $scope.prefixes.type = 'American Express'; break;
                case '4': $scope.prefixes.type = 'Visa'; break;
                case '5': $scope.prefixes.type = 'MasterCard'; break;
                case '6': $scope.prefixes.type = 'Discover'; break;
                default: $scope.prefixes.type = null; break;
            }
        };

    });

【问题讨论】:

  • 不用看太深,American Express 的空格让我有点怀疑。

标签: javascript html angularjs


【解决方案1】:

ng-init 中声明prefixes 变量时出现语法错误,您有一个带有空格的属性名称:American Express。因此,您需要用撇号将其括起来,以便 javascript 可以将其识别为属性名称,如下所示:

ng-init="prefixes={Visa: '4', MasterCard:'5', 'American Express':'3', Discover:'6'}"

下面的完整工作示例。

angular.module('edu_app', []).controller('edu_ctrl', function($scope) {
  $scope.prefixes = {};
  $scope.ccNumberChnage = function() {
    var ccType = $scope.prefixes.cards ? $scope.prefixes.cards.charAt(0) : '';
    switch (ccType) {
      case '3':
        $scope.prefixes.type = 'American Express';
        break;
      case '4':
        $scope.prefixes.type = 'Visa';
        break;
      case '5':
        $scope.prefixes.type = 'MasterCard';
        break;
      case '6':
        $scope.prefixes.type = 'Discover';
        break;
      default:
        $scope.prefixes.type = null;
        break;
    }
  };

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.js"></script>
<div ng-app="edu_app" ng-controller="edu_ctrl" ng-init="prefixes={Visa: '4', MasterCard:'5', 'American Express':'3', Discover:'6'}">
  <div>
    <div>
      <img id="visa" src="images/visa-card-logo.png" ng-class="{'greyed' : prefixes.type !== 'Visa'}" />
    </div>
    <div>
      <img id="mastercard" src="images/master-card-logo.png" ng-class="{'greyed' : prefixes.type !== 'MasterCard'}" />
    </div>
    <div>
      <img id="amex" src="images/amex-card-logo.png" ng-class="{'greyed' : prefixes.type !== 'American Express'}" />
    </div>
    <div>
      <img id="discover" src="images/discover-card-logo.png" ng-class="{'greyed' : prefixes.type !== 'Discover'}" />
    </div>
  </div>
</div>

【讨论】:

  • 啊啊啊我明白了。有趣的是,代码仍然有效,但我得到了这个语法错误,不知道为什么!但是谢谢你,你成功了。
  • @StevenSerrano 你得到语法错误并且代码仍然有效,因为它对ng-init 中的内容进行角度解析并将其应用于范围,一旦它运行时解析,它就不会停止javascript,但是 Angular 会抛出 Error: [$parse:syntax] ,就像在运行时尝试解析垃圾 javascript 的已处理异常一样。无论如何,我很高兴我能帮助你,。干杯:{D
猜你喜欢
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 2016-01-06
  • 2018-07-22
  • 2014-10-24
  • 1970-01-01
相关资源
最近更新 更多