1

I have some problem getting jQuery which i'm more familiar with to load html with ajax then add this to the view. The template is already loaded then I need to get some data externaly through ajax and put it inside element. But AngularJs runs before all this, I want it to wait until all data is loaded.

Edit: Just to clarify, here is the code, I run the custom.js.

 $stateProvider


    // Companyresults
    .state('companyresults', {
        url: "/companyresults.html",
        templateUrl: "views/companyresults.html",            
        data: {pageTitle: 'Dashboard', pageSubTitle: 'statistics & reports'},
        controller: "CompanyResultsController",
        resolve: {
            deps: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load({
                    name: 'MetronicApp',
                    insertBefore: '#ng_load_plugins_before', // load the above css files before '#ng_load_plugins_before'
                    files: [
                            '../../../assets/admin/layout3/scripts/custom.js',
                        'js/controllers/CompanyResultsController.js'
                    ] 
                });
            }]
        }
    });

And..

     'use strict';

    MetronicApp.controller('CompanyResultsController', function($rootScope, $scope, $http, $timeout) {
        $scope.$on('$viewContentLoaded', function() {   
       // initialize core components
       Metronic.initAjax();
   });
 });
Adrian McCool
  • 143
  • 2
  • 12
  • 2
    I will recommend you to read [__“Thinking in AngularJS” if I have a jQuery background?__](http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background/14994393#14994393) – Satpal Mar 29 '15 at 11:33
  • I would recommend loading AJAX data via Angular's $http service. – SamuelMS Jun 15 '15 at 08:42

2 Answers2

1

Instead of using ng-app directive to launch the app you should run angular.bootstrap(document, ['app']); when you think your document is ready (e.g. in jQuery callback);

Take notice that you can also use jQuery Ajax functions within Angular, but you need to wrap any changes in DOM with $scope.$apply to make Angular aware of jQuery's actions):

// within Angular controller
jQuery.get(url, function(data) {
    $scope.$apply(function() {
        // any DOM changes
    });
});

But consider (this would be the best solution) embedding ajax code in Angular, $http service doesn't differ too much from jQuery.ajax, especially if you're familiar with jQuery Promises.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
0

you can make custom directive and call your jquery plugin inside link function

  var directiveModule = angular.module("directiveModule", []);

    directiveModule.directive('wrapJqueryplugin', function() {
        return {
            restrict : 'E',
            link : function(scope, element, attrs) {        
    *********your jquery code ****************
});

and in your view

    <wrap-jqueryplugin>
***    any Dom Elements ***
    </wrap-jqueryplugin>
Ahmed Adel
  • 181
  • 1
  • 6
  • 15