• Blogs (9)
    • 📱 236 - 992 - 3846

      đź“§ jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • AngularJS tips

    Blogs20152015-07-16


    1. $scope.$on and element.on()

    $scope.$on() element.on() in link(scope, element, attrs)

    2. no need .$apply() in $timeout()

    $timeout(function () { $scope.assignmentsLoaded(data); }, 1000);
    //use $evalAsync() (which does work):
    $scope.$evalAsync(function() { $scope.assignmentsLoaded(data); } );

    Here’s the calling order: 1. app.config() 2. app.run() 3. directive’s compile functions (if they are found in the dom) 4. app.controller() 5. directive’s link functions (again if found)

    Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

    There are some convenience methods on the module which are equivalent to the config block. For example:

    angular.module('myModule', []).
      value('a', 123).
      factory('a', function() { return 123; }).
      directive('directiveName', ...).
      filter('filterName', ...);
    
    // is same as
    
    angular.module('myModule', []).
      config(function($provide, $compileProvider, $filterProvider) {
        $provide.value('a', 123);
        $provide.factory('a', function() { return 123; });
        $compileProvider.directive('directiveName', ...);
        $filterProvider.register('filterName', ...);
      });
    more: Module Loading & Dependencies

    1. Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured. 2. Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

    angular.module('myModule', []).
    config(function(injectables) { // provider-injector
      // This is an example of config block.
      // You can have as many of these as you want.
      // You can only inject Providers (not instances)
      // into config blocks.
    }).
    run(function(injectables) { // instance-injector
      // This is an example of a run block.
      // You can have as many of these as you want.
      // You can only inject instances (not Providers)
      // into run blocks
    });

    4. Add an angularjs directive into another directive

    inserting a new element(directive)//inserting a new attribute(directive) to element

    var newElement = $compile( "<div my-diretive='n'>" )( $scope );
    $element.parent().append( newElement );