gpt4 book ai didi

javascript - Angular 语法错误

转载 作者:行者123 更新时间:2023-12-01 03:00:33 25 4
gpt4 key购买 nike

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

});

最佳答案

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>

关于javascript - Angular 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46454290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com