AngularJS – Image “onload” event

Here’s a re-usable directive in the style of angular’s inbuilt event handling directives: angular.module(‘sbLoad’, []) .directive(‘sbLoad’, [‘$parse’, function ($parse) { return { restrict: ‘A’, link: function (scope, elem, attrs) { var fn = $parse(attrs.sbLoad); elem.on(‘load’, function (event) { scope.$apply(function() { fn(scope, { $event: event }); }); }); } }; }]); When the img load event … Read more

Angular: calling controller function inside a directive link function using &

Are you passing the arguments inside {}s? E.g., inside the directive’s link function, you’ll want to call the method like so: scope.someCtrlFn({arg1: someValue}); <div my-directive callback-fn=”ctrlFn(arg1)”></div> app.directive(‘myDirective’, function() { return { scope: { someCtrlFn: ‘&callbackFn’ }, link: function(scope, element, attrs) { scope.someCtrlFn({arg1: 22}); }, } }); function MyCtrl($scope) { $scope.ctrlFn = function(test) { console.log(test); } … Read more

Confused about Angularjs transcluded and isolate scopes & bindings

Your fiddles create three scopes: a scope associated with controller Ctrl, because of ng-controller a directive transcluded scope, because of transclude: true a directive isolate scope, because of scope: { … } In fiddle1, before we type anything into the text box we have the following: Scope 003 is the scope associated with the controller. … Read more

AngularJS ng-style with a conditional expression

simple example: <div ng-style=”isTrue && {‘background-color’:’green’} || {‘background-color’: ‘blue’}” style=”width:200px;height:100px;border:1px solid gray;”></div> {‘background-color’:’green’} RETURN true OR the same result: <div ng-style=”isTrue && {‘background-color’:’green’}” style=”width:200px;height:100px;border:1px solid gray;background-color: blue”></div> other conditional possibility: <div ng-style=”count === 0 && {‘background-color’:’green’} || count === 1 && {‘background-color’:’yellow’}” style=”width:200px;height:100px;border:1px solid gray;background-color: blue”></div>

Angularjs loading screen on ajax request

Instead of setting up a scope variable to indicate data loading status, it is better to have a directive does everything for you: angular.module(‘directive.loading’, []) .directive(‘loading’, [‘$http’ ,function ($http) { return { restrict: ‘A’, link: function (scope, elm, attrs) { scope.isLoading = function () { return $http.pendingRequests.length > 0; }; scope.$watch(scope.isLoading, function (v) { if(v){ … Read more

How to select an element by classname using jqLite?

Essentially, and as-noted by @kevin-b: // find(‘#id’) angular.element(document.querySelector(‘#id’)) //find(‘.classname’), assumes you already have the starting elem to search from angular.element(elem.querySelector(‘.classname’)) Note: If you’re looking to do this from your controllers you may want to have a look at the “Using Controllers Correctly” section in the developers guide and refactor your presentation logic into appropriate directives … Read more

Using angularjs filter in input element

In short: if you want your data to have a different representation in the view and in the model, you will need a directive, which you can think of as a two-way filter. Your directive would look something like angular.module(‘myApp’).directive(‘myDirective’, function() { return { require: ‘ngModel’, link: function(scope, element, attrs, ngModelController) { ngModelController.$parsers.push(function(data) { //convert … Read more