gpt4 book ai didi

javascript - 为什么 Angularjs 指令会变成 html 注释?

转载 作者:行者123 更新时间:2023-11-29 21:57:12 25 4
gpt4 key购买 nike

我正在尝试在从 .json 文件恢复的列表中重复

<ul class="phones">
<li ng-repeat="phone in phones | filter:query | orderBy:orderProp">
<span>{{phone.name}}</span>
<p>{{phone.snippet}}</p>
</li>
</ul>

但是当我加载页面时,它变成了 html 评论

<ul class="phones">
<!-- ngRepeat: phone in phones | filter:query | orderBy:orderProp -->
</ul>

网页

<!doctype html>
<html lang="en" ng-app="phonecatApp">
<head>
<meta charset="utf-8">
<title>Testing angularjs</title>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/app.css">
<script src="script/angular.js"></script>
<script src="script/controllers.js"></script>
</head>
<body ng-controller="PhoneListCtrl">
Search: <input ng-model="query">

<ul class="phones">
<li ng-repeat="phone in phones | filter:query | orderBy:orderProp">
<span>{{phone.name}}</span>
<p>{{phone.snippet}}</p>
</li>
</ul>
</body>
</html>

script/Controllers.js

'use strict';

/* Controllers */
var phonecatApp = angular.module('phonecatApp', []);

phonecatApp.controller('PhoneListCtrl', ['$scope', '$http',
function ($scope, $http) {
$http.get('data/data.json').success(function(data) {
$scope.phones = data;
});

$scope.orderProp = 'age';
}]);

data/data.json

[
{
"age": 11,
"id": "phone1",
"name": "es un tablet",
"snippet": "Are you here or not?"
},
{
"age": 12,
"id": "phone2",
"name": "this is a phone",
"snippet": "Are you sure?"
},
{
"age": 13,
"id": "phone3",
"name": "hello moto",
"snippet": "Are you seriously ready?"
},
]

控制台错误

XMLHttpRequest cannot load file:///D:/Dropbox/Web/Angularjs/testFromScratch/app/data/data.json. Cross origin requests are only supported for HTTP. 
Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///c://testFromScratch/app/data/data.json'.
at Error (native)
at file:///c://testFromScratch/app/script/angular.js:8553:11
at sendReq (file:///c://testFromScratch/app/script/angular.js:8347:9)
at $http.serverRequest (file:///c://testFromScratch/app/script/angular.js:8080:16)
at wrappedCallback (file:///c://testFromScratch/app/script/angular.js:11561:81)
at wrappedCallback (file:///c://testFromScratch/app/script/angular.js:11561:81)
at file:///c://testFromScratch/app/script/angular.js:11647:26
at Scope.$eval (file:///c://testFromScratch/app/script/angular.js:12673:28)
at Scope.$digest (file:///c://testFromScratch/app/script/angular.js:12485:31)
at Scope.$apply (file:///c://testFromScratch/app/script/angular.js:12777:24)

最佳答案

您需要 phones/phones.json 并且该文件似乎被标记为 data/data.json

注释是 Angular 标记其标记的方式(参见 Remove helper HTML comments in Angular JS? )。由于使用了错误的文件名,您没有包含实际数据,因此没有要列出的电话。当您更改文件名时,注释仍会存在,但下方会显示电话列表。

关于javascript - 为什么 Angularjs 指令会变成 html 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25866811/

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