gpt4 book ai didi

javascript - 为什么 ng-repeat 指令不起作用?

转载 作者:行者123 更新时间:2023-11-30 10:10:51 24 4
gpt4 key购买 nike

我正在学习 Angular 基础知识,但我无法理解为什么 ng-repeat 不起作用。

文件 app.js

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

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

});

var gems = [
{
name: "Dodecahedron",
price: 2.95,
description: '...',
canPurchase: true,
},
{
name: "Pentagonal Gem",
price: 5.95,
description: '...',
canPurchase: false,
}
];
})();

index.html

<!DOCTYPE html> 
<html ng-app="store">
<head>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</head>
<body ng-controller="StoreController as store">
<div ng-repeat="product in store.products">
<h1>{{store.product.name}}</h1>
<h2> ${{store.product.price}}</h2>
<p>{{store.product.description}}</p>
<button ng-show="store.product.canPurchase"> Add to Cart </button>
</div>
</body>
</html>

想补充一点,如果我尝试以非动态方式查看它(如 store.product[0].description),它会正常工作。

最佳答案

您正在 html 中使用 products 变量。所以替换为:

this.products=gems;

关于javascript - 为什么 ng-repeat 指令不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26815966/

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