gpt4 book ai didi

javascript - Angular 1.7 绑定(bind)数据问题

转载 作者:行者123 更新时间:2023-11-30 09:14:21 25 4
gpt4 key购买 nike

我正在尝试了解 Angular 1.7 中的绑定(bind)机制,因此我设置了一个简单的示例。

在此示例中,目的是为每个产品名称打印一个 h4 元素。

我的产品的 JSON 文件 (dummy.json)

{
"ProductName": "Test Product 1",
"QuantityPerUnit": "100"
}, {
"ProductName": "Test Product 2",
"QuantityPerUnit": "10"
}, {
"ProductName": "Test Product 3",
"QuantityPerUnit": "25"
}

在我的 ProductController 中,我想将我的 JSON 数据绑定(bind)到 $scope.products

ProductController.js

angular.module("angularApp", [])
.controller("ProductController", function ($scope) {

$.getJSON("./dummy.json", function (json) {
$scope.products = json;
});

});

在我的 HTML 中,我做了以下事情来使绑定(bind)工作:

  1. 定义应用ng-app="angularApp"

  2. 在 body-tag 中定义 Controller ng-controller="ProductController"

  3. 我想通过 ng-repeat="product in products" 循环访问我的产品,并且我正在使用数据绑定(bind)符号 Angular 说我必须使用 { { }}

index.html

<!DOCTYPE HTML>
<html ng-app="angularApp" lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<!-- Angular 1.7.8 -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.8/angular.min.js"></script>
<script src="./js/ProductController.js"></script>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<link rel="stylesheet" href="./css/style.css">
</head>

<body ng-controller="ProductController">
<div class="container">
<div class="list-group">
<h4 ng-repeat="product in products">{{ product.ProductName }}</h4>
</div>
</div>
</body>

</html>

控制台没有显示任何错误,当我检查我的开发者工具 (AngularJS Batarang) 时,我注意到在我的 body-tag 中存在我的 products

我忘记了什么或做错了什么?任何指向正确方向的指示都会受到赞赏。

提前致谢

最佳答案

第一个问题是 JSON 文件:它不是数组。

应该是这样的:

[{
"ProductName": "Test Product 1",
"QuantityPerUnit": "100"
}, {
"ProductName": "Test Product 2",
"QuantityPerUnit": "10"
}, {
"ProductName": "Test Product 3",
"QuantityPerUnit": "25"
}]

并且在评论中不确定您为什么使用 $getJson,但这应该有效:

angular.module("angularApp", [])
.controller("ProductController", function ($scope,$http) {
$http.get("dummy.json").then(function(response) {
console.log("Json",response.data);
$scope.products = response.data;
});
});

使用 Angular JS http 你应该能够获取数据,但你必须注入(inject) $http。

在这里您还可以找到 Plunker

关于javascript - Angular 1.7 绑定(bind)数据问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56222901/

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