Can an angular directive pass arguments to functions in expressions specified in the directive’s attributes?

If you declare your callback as mentioned by @lex82 like callback = “callback(item.id, arg2)” You can call the callback method in the directive scope with object map and it would do the binding correctly. Like scope.callback({arg2:”some value”}); without requiring for $parse. See my fiddle(console log) http://jsfiddle.net/k7czc/2/ Update: There is a small example of this in … Read more

Use of symbols ‘@’, ‘&’, ‘=’ and ‘>’ in custom directive’s scope binding: AngularJS

> is not in the documentation. < is for one-way binding. @ binding is for passing strings. These strings support {{}} expressions for interpolated values. = binding is for two-way model binding. The model in parent scope is linked to the model in the directive’s isolated scope. & binding is for passing a method into … Read more

Losing scope when using ng-include

As @Renan mentioned, ng-include creates a new child scope. This scope prototypically inherits (see dashed lines below) from the HomeCtrl scope. ng-model=”lineText” actually creates a primitive scope property on the child scope, not HomeCtrl’s scope. This child scope is not accessible to the parent/HomeCtrl scope: To store what the user typed into HomeCtrl’s $scope.lines array, … Read more

How do I ignore the initial load when watching model changes in AngularJS?

The first time the listener is called, the old value and the new value will be identical. So just do this: $scope.$watch(‘fieldcontainer’, function(newValue, oldValue) { if (newValue !== oldValue) { // do whatever you were going to do } }); This is actually the way the Angular docs recommend handling it: After a watcher is … Read more

How do I use $rootScope in Angular to store variables?

Variables set at the root-scope are available to the controller scope via prototypical inheritance. Here is a modified version of @Nitish’s demo that shows the relationship a bit clearer: http://jsfiddle.net/TmPk5/6/ Notice that the rootScope’s variable is set when the module initializes, and then each of the inherited scope’s get their own copy which can be … 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

How to set the id attribute of a HTML element dynamically with angularjs (1.x)?

ngAttr directive can totally be of help here, as introduced in the official documentation https://docs.angularjs.org/guide/interpolation#-ngattr-for-binding-to-arbitrary-attributes For instance, to set the id attribute value of a div element, so that it contains an index, a view fragment might contain <div ng-attr-id=”{{ ‘object-‘ + myScopeObject.index }}”></div> which would get interpolated to <div id=”object-1″></div>

$watch an object

Call $watch with true as the third argument: $scope.$watch(‘form’, function(newVal, oldVal){ console.log(‘changed’); }, true); By default when comparing two complex objects in JavaScript, they will be checked for “reference” equality, which asks if the two objects refer to the same thing, rather than “value” equality, which checks if the values of all the properties of … Read more

$rootScope.$broadcast vs. $scope.$emit

tl;dr (this tl;dr is from @sp00m’s answer below) $emit dispatches an event upwards … $broadcast dispatches an event downwards Detailed explanation $rootScope.$emit only lets other $rootScope listeners catch it. This is good when you don’t want every $scope to get it. Mostly a high level communication. Think of it as adults talking to each other … Read more

AngularJS access parent scope from child controller

If your HTML is like below you could do something like this: <div ng-controller=”ParentCtrl”> <div ng-controller=”ChildCtrl”> </div> </div> Then you can access the parent scope as follows function ParentCtrl($scope) { $scope.cities = [“NY”, “Amsterdam”, “Barcelona”]; } function ChildCtrl($scope) { $scope.parentcities = $scope.$parent.cities; } If you want to access a parent controller from your view you … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)