0

I have a webmethod on my remote server returning a JSon object. Said json object contains strings which are required for the module to proper work.

So, is there a way to pass something to an angular module? An idea could be to perform an http request inside of the initialization of the module:

$scope.init = function(){
        $http.get ()
            ...
         then(){
               $scope.mydata = result;
         }

};

But that would be asynchronous...

3
  • why not pass a service. that can provide you with that end point? Commented Dec 21, 2015 at 17:37
  • mmm...an example of that? Commented Dec 21, 2015 at 17:46
  • I'm doing exactly that to initialize schemaform.io form definition and schema JSONobjects. If you are using a third party module you can trigger an event to reinitialize on your $http.get() success or if it's your own module, create a function that will reinitialize the module with the new data. Commented Dec 21, 2015 at 17:47

1 Answer 1

1

If you are worried about the async nature of an HTTP request to fill out your $scope.mydata variable, then you need to include this in a RESOLVE in your route.

I'm using UI-ROUTER in my current project:

(function() {
    'use strict';

    angular
        .module('capps.core')
        .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
            $urlRouterProvider.otherwise('/');

            $stateProvider
                .state('home.capps', {
                    url: '/',
                    template: '<ui-view/>',
                    data: {
                        requireLogin: true
                    },
                    resolve:{
                        resolveFunction: resolveFunction
                    }
                });
        }
    ]);

    resolveFunction['$inject'] = ['$http'];
    function resolveFunction($http) {
        return $http.get(API_URL)
                .then(function(res) {
                    console.log(res);
                });
    }
})();

Then in your controller, you can pass 'resolveFunction' as a dependency... then use that to assign to your $scope.myData.

...
angular.controller('myController', myController);

myController.$inject = ['resolveFunction', '$scope'];
function myController(resolveFunction, $scope) {
    $scope.mydata = resolveFunction.data;
    console.log(mydata);
};
Sign up to request clarification or add additional context in comments.

1 Comment

There are other ways of doing this like Suda said... personally I would contain all of the REST interaction inside a service. Then within the resolve I would call a loadMyData() function to GET the data from the API... then in the controller you know the data is already initialized in the service. So you can set ... $scope.myData = myDataService.getMyData(); (this is not an API call but a reference to the singleton service that contains the data it already pulled from the API).

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.