gpt4 book ai didi

javascript - Angular.js ng-repeat 问题

转载 作者:行者123 更新时间:2023-12-03 06:45:53 25 4
gpt4 key购买 nike

我刚刚开始使用 Angular,并且正在关注 CodeSchool 的视频。第一部分进行得很顺利,但是当尝试复制数组的“ng-repeat”部分时,html 不显示对象的值,只显示指令。

app.js:

(function () {
var app = angular.module('store', []);

app.controller('StoreController', function(){
this.products = gems;


});

var gems = {
{
name: 'Dodecaherdron',
price: 2.95,
description: 'This is a Dodecahedron',
canPurchase: true,
},
{
name: "Pentagonal Gem",
price: 5.95,
description: "This is a Pentagonal Gem",
canPurchase: true,
}
}
})();

html:

<body ng-controller="StoreController as store">
<div ng-repeat="product in store.products">
<h1>{{product.name}} </h1>
<h2>${{product.price}}</h2>
<p>{{product.description}}</p>
<button ng-show="product.canPurchase">Add to Cart</button>
</div>


<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>

同样,当尝试使用单个 gem 作为产品时,代码有效,但当我添加第二个“五 Angular 形 gem ”时,代码不起作用,即使我逐字检查视频的代码。 html 站点准确显示 html 文档中 h1、h2 和 h3 标题下的内容。帮助将不胜感激。

最佳答案

您需要将 gem 设为array rather than an object .

var gems = [{
name: 'Dodecaherdron',
price: 2.95,
description: 'This is a Dodecahedron',
canPurchase: true,
}, {
name: "Pentagonal Gem",
price: 5.95,
description: "This is a Pentagonal Gem",
canPurchase: true,
}];

关于javascript - Angular.js ng-repeat 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37738643/

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