DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. AngularJS: Resolving data services before instantiating the Controller and Template

AngularJS: Resolving data services before instantiating the Controller and Template

Sagar Ganatra user avatar by
Sagar Ganatra
·
Jul. 01, 14 · Interview
Like (0)
Save
Tweet
Share
47.78K Views

Join the DZone community and get the full member experience.

Join For Free

I have been working on an Angular application for sometime now and I have started to like this approach to client-side development. It's a different approach when compared to developing applications using Backbone and Require. I was looking at routing in Angular and came across the 'resolve' property which can be used to resolve services before instantiating the Controller and it's corresponding template i.e. the domain models that are required for the View components are resolved first.

Look at a scenario where you are using a Search service to get a collection of objects that contain the search string. You can specify the route which will display the search results. When only the template and controller properties are specified, both are instantiated and the ng-view is updated with the specified template. The controller  would specify dependency on the SearchService,use this service to query the back-end and then update one of the $scope variables with the response data.

While the SearchService is fetching results, you would see that the template being displayed inside the ng-view container with template variables and the same getting updated when the service returns data. This is not ideal; the search results should be available before you load the controller; and the template should display data instead of showing template variables. The 'resolve' property in the $routeProvider mentions a set of promises that should be resolved before instantiating the controller and the template. Only when all the promises are resolved, the controller and the template are instantiated. Also the promise reference is injected into the controller as a dependency. Using this promise one can directly update the values in the template instead of sending a request to the Search service.

Here's the code for the routeProvider:

window.app = angular.module('routeResolve', [
    'ngRoute',
    'mockService'
]);

window.appMock = angular.module('mockService', ['ngMockE2E']);

window.app.config([
    '$routeProvider',
    function ($routeProvider) {
        $routeProvider
            .when('/search/:searchString', {
                templateUrl: 'partials/search.html',
                controller: 'searchController',
                resolve: {
                    searchResults: ['$route', 'searchService', function ($route, searchService) {
                        return searchService.getSearchResults($route.current.params);
                    }]
                }
            });

    }
]);

Here the route '/search/:searchString', specifies the 'resolve' object which includes a promise - 'searchResults'. It queries the searchService and returns a promise. When the service is resolved the Controller - 'searchController' and the template - 'partials/search.html' are instantiated. The Controller gets the promise object:

window.app.controller('searchController', [
    '$scope', 'searchResults',
    function ($scope, searchResults) {
        console.log('Inside Search Controller');
        $scope.userCollection = searchResults.data;
    }
]);

Notice that the promise - 'searchResults' is injected into the Controller and it contains the response data. This can then be used to render the template.



Template Data (computing) AngularJS

Published at DZone with permission of Sagar Ganatra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Factors When Selecting a Database
  • PHP vs React
  • Top 5 Java REST API Frameworks
  • Key Considerations When Implementing Virtual Kubernetes Clusters

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: