【发布时间】:2020-04-09 23:05:25
【问题描述】:
我正在学习 angularJs,我想从我的控制器上的 json 导入一个数组就像这样:
myApp.controller("demoCtrl", function ($scope, $http) {
var promise = $http.get("todo.json");
promise.then(function (data) {
$scope.todos = data;
});
});
我使用表格来显示todos上的数据:
<table class="table">
<tr>
<td>Action</td>
<td>Done</td>
</tr>
<tr ng-repeat="item in todos">
<td>{{item.action}}</td>
<td>{{item.done}}</td>
</tr>
</table>
这会在流动的 html 页面上产生:
<!DOCTYPE html>
<html ng-app="demo">
<head>
<title>Example</title>
<link href="../css/bootstrap.css" rel="stylesheet" />
<link href="../css/bootstrap-theme.css" rel="stylesheet" />
<script src="angular.js"></script>
<script type="text/javascript">
var myApp = angular.module("demo", []);
myApp.controller("demoCtrl", function ($scope, $http) {
var promise = $http.get("todo.json");
promise.then(function (data) {
$scope.todos = data;
});
});
</script>
</head>
<body ng-controller="demoCtrl">
<div class="panel">
<h1>To Do</h1>
<table class="table">
<tr>
<td>Action</td>
<td>Done</td>
</tr>
<tr ng-repeat="item in todos">
<td>{{item.action}}</td>
<td>{{item.done}}</td>
</tr>
</table>
</div>
</body>
【问题讨论】:
-
控制台(浏览器)是否有错误? 'ng-app', 'ng-controller' 在 html 正文中吗?
-
@Mostav 我注意到我忘记了 ng-app,但我修复了它,现在只显示空的
标签 如果可能的话,你能把整个 html 和 js 文件贴出来吗?没有所有必要元素的问题根本无济于事。@Mostav 我已经编辑了帖子