我正在创建一个个人网站,我可以继续更新内容,而无需操纵HTML.我试图通过简单地加载和更新JSON文件来实现这一点.但是现在,我无法将JSON数据加载到scope变量上.
HTML
<!doctype html>
<html>
<head>
<script src="angular.min.js" type="text/javascript"></script>
<script src="maincontroller.js" type="text/javascript"></script>
</head>
<body>
<div ng-app="mainApp" ng-controller="mainController">
<div id="content">
<div ng-repeat="content in contents">
<h2>{{content.heading}}</h2>
<p>{{content.description}}</h2>
</div>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
maincontroller.js
var myapp = angular.module('mainApp', []);
myapp.controller('mainController',function($scope,$http){
$scope.contents = null;
$http.get('mainContent.json')
.success(function(data) {
$scope.contents=data;
})
.error(function(data,status,error,config){
$scope.contents = [{heading:"Error",description:"Could not load json data"}];
});
//$scope.contents = [{heading:"Content heading", description:"The actual content"}];
//Just a placeholder. All web content will be in this format …Run Code Online (Sandbox Code Playgroud) 我是AngularJS的新手.我只想将JSON文件数据加载到位于工厂中的变量中.
myApp.factory('quizFactory', function () {
var questions = [
{
"setId":1,
"question":"What is the value of the 7 in the following number? 850,765",
"options": ["70", "7", "7000", "700"],
"answer": 3
},
{
"setId":2,
"question":"Is 7 & 9 even or odd?",
"options": ["Even", "Odd", "Can't Say", "Dont know"],
"answer": 1
},
{
"setId":3,
"question":"In the number 5,281,946 what is the value of the 3rd place?",
"options": ["100", "10,000", "1,000,000", "1,000"],
"answer": 0
},
{
"setId":4,
"question":"Is 12 + 50 even or …Run Code Online (Sandbox Code Playgroud)