Ruben Alonzo Ruben Alonzo - 2 months ago 22
AngularJS Question

Trouble accesing JSON file with angular

This is the JSON and controllers im using:

JSON Format:

{"tomcatLogDir":"C:\\apache-tomcat-7.0.70\\logs","tomcatLogs":["1.log","2.log","3.log"]}


Factory:

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;
})
} ]);


Controller:

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>


So i tried this, and the tomcatLogs don't appear they seem to not get loaded into the html.

What I'm I doing wrong, I'm i doing the wrong call to the JSON file?

Thank you.

Answer

Check your controller, you should have 'filesFactory' instead of jsonFactory

It should have parameters correctly,

app.controller('tomcatLogDirController', [ '$scope', 'filesFactory',
                                            function($scope, filesFactory) {

EDIT

  <ul>
   <li ng-repeat="x in tomcatDir.tomcatLogs">
     <a >{{x}}</a>
   </li>
  </ul>

DEMO