0

i want to pass a json file or object to an angular controller to use it with ng-repeat.

My json object is stored in my index.js file and written to data.json. My controller.js file looks like the following:

var fs = require('fs');

var jobs = fs.readFile('out/data.json', 'utf-8', (err, data) => {
    if (err) throw err;
});

angular.module('slrm', [].controller('slrmctrl', function($scope) {
    $scope.data = jobs.data;
}));

And thats my html file:

<table class="table">
        <thead>
            <th scope="col">#</th>
            <th scope="col">JOBNAME</th>
            <th scope="col">USER</th>
            <th scope="col">NODE</th>
            <th scope="col">CPUS</th>
            <th scope="col">START</th>
            <th scope="col">STATE</th>
            </thead>
            <tbody ng-app="slrm" ng-controller="slrmctrl">
                <tr ng-repeat="x in data | orderBy : 'JOBID'">
                    <td>{{ x.JOBID }}</td>
                    <td>{{ x.NAME }}</td>
                    <td>{{ x.USER }}</td>
                    <td>{{ x.NODELIST }}</td>
                    <td>{{ x.CPUS }}</td>
                    <td>{{ x.STATE }}</td>
                    <td>{{ x.REASON }}</td>
                </tr>
            </tbody>
    </table>
    <script src="js/controller.js"></script>

Now I have 2 questions. Im providing this html with:

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/view/index.html');
});

var server = app.listen(3000, function() {
    console.log('Server running');
};

Is the controller.js file even imported? because bootstrap.css is not imported somehow.

The other Question is if the line

$scope.data = jobs.data;

is even working? Should I read the file or use the exported object from index.js? How do I export only this one object?

I built this from scratch because im very new to js and stuff.

Thank you!

floriantaut
  • 347
  • 1
  • 2
  • 11

1 Answers1

0

I think the issue lies in your module and controller declaration. When declaring a module, you have to instantiate it first, by passing it's dependencies.

angular.module('slrm', []);

Afterwards, you can attach a controller to the module

angular.module('slrm').controller('slrmctrl', function($scope) { $scope.data = jobs.data; }));

Secondly, instead of reading the data.json object externally and passing it to controller, why don't you read the data inside your controller?

angular.module('slrm').controller('slrmctrl', ['$scope', '$http', function($scope, $http) {
    $http.get('out/data.json')
        .then(function(response) {
            $scope.data = response.data;
        }); 
}]);

EDIT to show example of timer (polling) implementation

angular.module('slrm').controller('slrmctrl', ['$scope', '$http', '$timeout', function($scope, $http, $timeout) {
    $scope.refreshInterval = 900; // Sec (15 Minutes)

    $scope.getData = function () {
        $http.get('out/data.json')
            .then(function(response) {
                $scope.data = response.data;

                return $timeout($scope.getData, $scope.getTimeLeftToRefresh());
            }); 
    };

    $scope.getTimeLeftToRefresh = function () {
        var now = new Date();
        var timeLeftToRefresh = ($scope.refreshInterval - (now.getMinutes() * 60 + now.getSeconds()) % $scope.refreshInterval) * 1000;

        return timeLeftToRefresh;
    };

    // Initial call to start the loop
    $scope.getData();

}]);
Protozoid
  • 1,207
  • 1
  • 8
  • 16
  • Thank you. I can't try out if its working yet because I still have the problem that my controller.js and bootstrap aren't imported. Can you help me with this too? The paths are correct because, when I open my index.html in safari, the bootstrap-css appears – floriantaut May 18 '18 at 17:56
  • somehow its not working, the data does not appear in my table. do you have any other solution for my problem? – floriantaut Jun 04 '18 at 10:50
  • What do you see in the browser console if you add a `console.log(response);` after `$scope.data = response.data;`? – Protozoid Jun 04 '18 at 13:20
  • nothing. it seems like the angular module is not even called. – floriantaut Jun 04 '18 at 13:25
  • Have you imported your files in `index.html`? E.G. `` – Protozoid Jun 04 '18 at 13:37
  • yes, did it the exact same way you mentioned a second ago but i still get no output and no data in my html. do i need to import angular first somehow? – floriantaut Jun 04 '18 at 13:39
  • Of course you should have `` first. – Protozoid Jun 04 '18 at 13:40
  • which npm package do i need to install then, angular2 or angularjs? – floriantaut Jun 04 '18 at 13:41
  • AngularJS, you should install it via bower if possible. – Protozoid Jun 04 '18 at 13:42
  • why is bower important? havent worked with it yet only npm and yarn – floriantaut Jun 04 '18 at 13:42
  • Bower is just like NPM but more suitable for front-end library packages. I'm sure NPM will do too – Protozoid Jun 04 '18 at 13:44
  • ok, now i dont see the `{{x.JOBID}}` tags anymore, but i still dont see any data and no output in the console. probably the `$http.get(__dirname + '/out/data.json')` command doenst work? – floriantaut Jun 04 '18 at 13:52
  • it was yes. i exported the `/out` folder with `express.static()` and i now see data in my html! thank you very much! no i have another question about automatically reread the `data.json` every 15 minutes which is generated by a cronjob. is there any option or should i just create a daemon for my node app and restart it every 15 minutes? – floriantaut Jun 04 '18 at 13:56
  • Well done :) You can write a scheduler for when the `$http.get` should trigger. I assume you want fixed times e.g. 12:00, 12:15, 12:30 etc.? – Protozoid Jun 04 '18 at 13:58
  • Also please mark the answer as preferred if it fixed your issues – Protozoid Jun 04 '18 at 13:59
  • yes correct. probably this fits me needs? https://stackoverflow.com/questions/20499225/i-need-a-nodejs-scheduler-that-allows-for-tasks-at-different-intervals – floriantaut Jun 04 '18 at 14:00
  • That could probably work, but because it's a backend change, it will require a browser refresh to load the latest re-build version of the page. I'll edit the comment to show a frontend-end timer implementation – Protozoid Jun 04 '18 at 14:04
  • A refresh of the browser would be fine but thank you for all you tips and help at all! – floriantaut Jun 04 '18 at 14:05
  • No problem man, good luck with the rest of it. I've edited the answer to show an example of a scheduler refreshing the data at 00, 15, 30, 45 mins – Protozoid Jun 04 '18 at 14:19
  • Thank you your solution is working, but the problem is, until the first refresh is running no data is shown, do you have any solution for this – floriantaut Jun 04 '18 at 17:34
  • Oups, sorry, I shouldn't have wrapped the initial call in a timeout, try now, I've updated the answer – Protozoid Jun 04 '18 at 17:59
  • Thank you I updated it and it works. I have one last question. At the moment im reading the jobs from /out/data.json inside of my projects folder. But I want to run my app with a docker container so I don't need to install node on my machine. How can I read the data.json file from f.e. /var/www/html/data.json? Searching google didn't bring me that far. – floriantaut Jun 05 '18 at 07:09
  • Hey @floriantaut, I'm not a docker expert but I think what you need to solve is "how to share files between docker and host" ... one of the classic problems of virtual machines too. Maybe this helps https://stackoverflow.com/questions/30652299/having-docker-access-external-files ? – Protozoid Jun 05 '18 at 08:11
  • Thank you will try it out. I just noticed that we or I have forgotten something :D, data.json is reread every f.e. 15 minutes, but its not even regenerated from the textfile, it rereads just the existing data.json which stays the same until I restart the whole app. Can I use the same code for rereading the my textile and parse it to json? – floriantaut Jun 05 '18 at 10:03
  • Yeah I don't see why not – Protozoid Jun 05 '18 at 10:05