Easiest way to pass an AngularJS scope variable from directive to controller?

Edited on 2014/8/25: Here was where I forked it. Thanks @anvarik. Here is the JSFiddle. I forgot where I forked this. But this is a good example showing you the difference between = and @ <div ng-controller=”MyCtrl”> <h2>Parent Scope</h2> <input ng-model=”foo”> <i>// Update to see how parent scope interacts with component scope</i> <br><br> <!– attribute-foo … Read more

Angular2 – Input Field To Accept Only Numbers

You can use angular2 directives. Plunkr import { Directive, ElementRef, HostListener, Input } from ‘@angular/core’; @Directive({ selector: ‘[OnlyNumber]’ }) export class OnlyNumber { constructor(private el: ElementRef) { } @Input() OnlyNumber: boolean; @HostListener(‘keydown’, [‘$event’]) onKeyDown(event) { let e = <KeyboardEvent> event; if (this.OnlyNumber) { if ([46, 8, 9, 27, 13, 110, 190].indexOf(e.keyCode) !== -1 || // … Read more

How to set bootstrap navbar active class with Angular JS?

A very elegant way is to use ng-controller to run a single controller outside of the ng-view: <div class=”collapse navbar-collapse” ng-controller=”HeaderController”> <ul class=”nav navbar-nav”> <li ng-class=”{ active: isActive(“https://stackoverflow.com/”)}”><a href=”https://stackoverflow.com/”>Home</a></li> <li ng-class=”{ active: isActive(‘/dogs’)}”><a href=”http://stackoverflow.com/dogs”>Dogs</a></li> <li ng-class=”{ active: isActive(‘/cats’)}”><a href=”http://stackoverflow.com/cats”>Cats</a></li> </ul> </div> <div ng-view></div> and include in controllers.js: function HeaderController($scope, $location) { $scope.isActive = function (viewLocation) … Read more

Angularjs dynamic ng-pattern validation

This is an interesting problem, complex Angular validation. The following fiddle implements what you want: http://jsfiddle.net/2G8gA/1/ Details I created a new directive, rpattern, that is a mix of Angular’s ng-required and the ng-pattern code from input[type=text]. What it does is watch the required attribute of the field and take that into account when validating with … Read more

When writing a directive in AngularJS, how do I decide if I need no new scope, a new child scope, or a new isolated scope?

What a great question! I’d love to hear what others have to say, but here are the guidelines I use. The high-altitude premise: scope is used as the “glue” that we use to communicate between the parent controller, the directive, and the directive template. Parent Scope: scope: false, so no new scope at all I … Read more

What is the best way to conditionally apply attributes in AngularJS?

I am using the following to conditionally set the class attr when ng-class can’t be used (for example when styling SVG): ng-attr-class=”{{someBoolean && ‘class-when-true’ || ‘class-when-false’ }}” The same approach should work for other attribute types. (I think you need to be on latest unstable Angular to use ng-attr-, I’m currently on 1.1.4)

Angular.js directive dynamic templateURL

emanuel.directive(‘hymn’, function() { return { restrict: ‘E’, link: function(scope, element, attrs) { // some ode }, templateUrl: function(elem,attrs) { return attrs.templateUrl || ‘some/path/default.html’ } } }); So you can provide templateUrl via markup <hymn template-url=”contentUrl”><hymn> Now you just take a care that property contentUrl populates with dynamically generated path.