angularjs读取本地json

1、允许浏览器读取本地数Google Chrome

方法a、 Google Chrome应用程序目标位置中添加  chrome.exe --allow-file-access-from-files  

! 注意中间空格(chrome.exe  --allow-file-access-from-files)

注意!!!   必须重新启动电脑,否则无效......若方案一还无效,继续执行方案二

方法b、 使用cmd命令 传入浏览器启动参数 --allow-file-access-from-files

方法c、用启动浏览器打开html文件即可. 若还出现上述问题,重启电脑,继续方案二

2、生成json文件

重命名areaList.json

3、编辑angularjs目录

4、源码

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
<title>无标题文档</title>
<script src="./static/index.js"></script>
</head>

<body>
<div ng-app="indexApp" ng-controller="indexCtrl">
    名字: <input ng-model="name">
    <h1>你输入了: {{name}}</h1>
</div>
</body>
</html>

index.js

var app = angular.module('indexApp', []);
app.controller('indexCtrl', function($scope,$http) {
    $http.get('static/areaList.json').success(function (data) {
        $scope.nick = data;
    });
    $scope.name = "John 111";
});// JavaScript Document

areaList.json

[
    { "id": 1, "username": "situ", "gender": "男", "email": "[email protected]" },
    { "id": 2, "username": "wb", "gender": "女", "email": "[email protected]" },
    { "id": 3, "username": "lml", "gender": "男", "email": "[email protected]" },
    { "id": 4, "username": "wjd", "gender": "女", "email": "[email protected]" },
    { "id": 5, "username": "lyl", "gender": "男", "email": "[email protected]" },
    { "id": 6, "username": "wjh", "gender": "女", "email": "[email protected]" }
]

猜你喜欢

转载自my.oschina.net/u/2441766/blog/1785814
今日推荐