1

I am trying to call a node function when loading a page with angular, but for some reason the function does not get called. I have the ng-app and controller specified and I figured I would just put the api call in the controller constructor. Here is the code for the page:

<!doctype html>

<!-- ASSIGN OUR ANGULAR MODULE -->
<html ng-app="landingPage">
<head>
    <!-- META -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1"><!-- Optimize mobile viewport -->

    <title>my page</title>

    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
    <script src="core.js"></script> 

</head>
<!-- SET THE CONTROLLER AND GET ALL GATEWAYS -->
<body ng-controller="mainController">

And then my core.js file is in the same directory which holds the controller has the controller:

var loadingCtrl = angular.module('landingPage', []);

function mainController($scope, $http) {
    console.log('loading gateways');

    // when landing on the page, get all gateways and show them
    $http.get('/api/gateways')
        .success(function(data) {
            $scope.gateways = data;
            console.log('got response');
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });

}

I never see any of the log statements....

2 Answers 2

1

I think you just need to change

function mainController($scope, $http) {
  //your code here
}

to

angular.module('landingPage').controller('mainController', function($scope, $http) {
   //your code here
});

To let angular know the controller exists.

Sign up to request clarification or add additional context in comments.

1 Comment

other option is angular.module("landingPage").controller("mainController", mainController); after the named function is defined.
0

Try array notation for dependency injection like below.

angular.module('landingPage', []).controller('mainController',['$scope','$http', function($scope, $http) {
    console.log('loading gateways');

    // when landing on the page, get all gateways and show them
    $http.get('/api/gateways')
        .success(function(data) {
            $scope.gateways = data;
            console.log('got response');
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
}]);

Check the Plunker. But I would suggest to use factory service for loading external data instead loading it inside controller as you can reuse the same service in different controllers.

Sample code with factory service

'use strict';
angular.module('landingPage', []).controller('mainController',['$scope','$http','gatewayService', function($scope, $http,gatewayService) {
    console.log('loading gateways');

    // when landing on the page, get all gateways and show them

    $scope.gateways = gatewayService.getData();

}]).factory('gatewayService', ['$http',function($http){
    return {
        getData: function() {
            return $http.get('/api/gateways')
              .success(function(data) {
                  return data
                  console.log('got response');
                  console.log(data);
              })
              .error(function(data) {
                  console.log('Error: ' + data);
                  return data;
              });
        }
    };
}]);

Check Plunkr

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.