Angular directives – when and how to use compile, controller, pre-link and post-link [closed]

In which order the directive functions are executed? For a single directive Based on the following plunk, consider the following HTML markup: <body> <div log=’some-div’></div> </body> With the following directive declaration: myApp.directive(‘log’, function() { return { controller: function( $scope, $element, $attrs, $transclude ) { console.log( $attrs.log + ‘ (controller)’ ); }, compile: function compile( tElement, … Read more

How to use a keypress event in AngularJS?

You need to add a directive, like this: Javascript: app.directive(‘myEnter’, function () { return function (scope, element, attrs) { element.bind(“keydown keypress”, function (event) { if(event.which === 13) { scope.$apply(function (){ scope.$eval(attrs.myEnter); }); event.preventDefault(); } }); }; }); HTML: <div ng-app=”” ng-controller=”MainCtrl”> <input type=”text” my-enter=”doSomething()”> </div>

Angular HttpPromise: difference between `success`/`error` methods and `then`’s arguments

There are some good answers here already. But it’s worthwhile to drive home the difference in parallelism offered: success() returns the original promise then() returns a new promise The difference is then() drives sequential operations, since each call returns a new promise. $http.get(/*…*/). then(function seqFunc1(response){/*…*/}). then(function seqFunc2(response){/*…*/}) $http.get() seqFunc1() seqFunc2() success() drives parallel operations, since … Read more

How to call a method defined in an AngularJS directive?

If you want to use isolated scopes you can pass a control object using bi-directional binding = of a variable from the controller scope. You can also control also several instances of the same directive on a page with the same control object. angular.module(‘directiveControlDemo’, []) .controller(‘MainCtrl’, function($scope) { $scope.focusinControl = {}; }) .directive(‘focusin’, function factory() … Read more

Running Angular and AngularJS frameworks side by side

Incrementally upgrade an AngularJS application to Angular. One of the keys to a successful upgrade is to do it incrementally, by running the two frameworks side by side in the same application, and porting AngularJS components to Angular one by one. This makes it possible to upgrade even large and complex applications without disrupting other … Read more

How to access parent scope from within a custom directive *with own scope* in AngularJS?

See What are the nuances of scope prototypal / prototypical inheritance in AngularJS? To summarize: the way a directive accesses its parent ($parent) scope depends on the type of scope the directive creates: default (scope: false) – the directive does not create a new scope, so there is no inheritance here. The directive’s scope is … Read more