使用 angular 访问 JSON 文件时遇到问题
Trouble accesing JSON file with angular
这是我使用的 JSON 和控制器:
JSON格式:
{"tomcatLogDir":"C:\apache-tomcat-7.0.70\logs","tomcatLogs":["1.log","2.log","3.log"]}
工厂:
app.factory('filesFactory', [ '$http',
function($http) {
return $http.get("http://"+ boxIP +":"+ boxPort +"/monitorTool/rest/toolservice/files")
.success(function(data) {
return data;
}).error(function(err) {
return err;
})
} ]);
控制器:
app.controller('tomcatLogDirController', [ '$scope', 'filesFactory',
function($scope, jsonFactory) {
jsonFactory.success(function(data) {
$scope.tomcatDir = data;
});
}]);
HTML:
<div ng-controller="tomcatLogDirController">
<ul>
<li ng-repeat="x in tomcatDir">
<a ng-href="{{'file?name=' + tomcatDir.tomcatLogDir + x.tomcatLogs}}">{{x.tomcatLogs}}</a>
</li>
</ul>
</div>
所以我尝试了这个,但 tomcatLogs 没有出现,它们似乎没有加载到 html。
我做错了什么,我对 JSON 文件的调用错误?
谢谢。
检查你的控制器,你应该有 'filesFactory'
而不是 jsonFactory
应该有正确的参数,
app.controller('tomcatLogDirController', [ '$scope', 'filesFactory',
function($scope, filesFactory) {
编辑
<ul>
<li ng-repeat="x in tomcatDir.tomcatLogs">
<a >{{x}}</a>
</li>
</ul>
这是我使用的 JSON 和控制器:
JSON格式:
{"tomcatLogDir":"C:\apache-tomcat-7.0.70\logs","tomcatLogs":["1.log","2.log","3.log"]}
工厂:
app.factory('filesFactory', [ '$http',
function($http) {
return $http.get("http://"+ boxIP +":"+ boxPort +"/monitorTool/rest/toolservice/files")
.success(function(data) {
return data;
}).error(function(err) {
return err;
})
} ]);
控制器:
app.controller('tomcatLogDirController', [ '$scope', 'filesFactory',
function($scope, jsonFactory) {
jsonFactory.success(function(data) {
$scope.tomcatDir = data;
});
}]);
HTML:
<div ng-controller="tomcatLogDirController">
<ul>
<li ng-repeat="x in tomcatDir">
<a ng-href="{{'file?name=' + tomcatDir.tomcatLogDir + x.tomcatLogs}}">{{x.tomcatLogs}}</a>
</li>
</ul>
</div>
所以我尝试了这个,但 tomcatLogs 没有出现,它们似乎没有加载到 html。
我做错了什么,我对 JSON 文件的调用错误?
谢谢。
检查你的控制器,你应该有 'filesFactory'
而不是 jsonFactory
应该有正确的参数,
app.controller('tomcatLogDirController', [ '$scope', 'filesFactory',
function($scope, filesFactory) {
编辑
<ul>
<li ng-repeat="x in tomcatDir.tomcatLogs">
<a >{{x}}</a>
</li>
</ul>