Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What is the easiest way to pass an AngularJS scope variable from directive to controller? All of the examples that I've seen seem so complex, isn't there a way I can access a controller from a directive, and set one of it's scope variables?

like image 663
winduptoy Avatar asked Nov 10 '12 02:11

winduptoy


People also ask

How do you access the directive variable in a controller?

You just create a myVar variable in your controller and pass it to the directive using my-var attribute. Since you are using two way binding, any changes made to myVar by the directive are available in your controller.

How do you share data between controller and view in AngularJS?

To create a module in AngularJS, we use angular. module("app", []); syntax. 14) Which of the following is used to share data between controller and view in AngularJS? Answer: B: "using services" is the correct answer.

What is the best scope to be used for reusable directives in AngularJS?

Isolate Scope: If the need is to reuse the component (directive) throughout your app, consider creating isolate scopes using scope option. The concept of isolate scope is used to separate the scope inside a directive from the scope outside.

How I use scope in a directive AngularJS?

The directive scope uses prefixes to achieve that. Using prefixes helps establish a two-way or one-way binding between parent and directive scopes, and also make calls to parent scope methods. To access any data in the parent scope requires passing the data at two places – the directive scope and the directive tag.


1 Answers

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 binds to a DOM attribute which is always     a string. That is why we are wrapping it in curly braces so     that it can be interpolated. -->     <my-component attribute-foo="{{foo}}" binding-foo="foo"         isolated-expression-foo="updateFoo(newFoo)" >         <h2>Attribute</h2>         <div>             <strong>get:</strong> {{isolatedAttributeFoo}}         </div>         <div>             <strong>set:</strong> <input ng-model="isolatedAttributeFoo">             <i>// This does not update the parent scope.</i>         </div>         <h2>Binding</h2>         <div>             <strong>get:</strong> {{isolatedBindingFoo}}         </div>         <div>             <strong>set:</strong> <input ng-model="isolatedBindingFoo">             <i>// This does update the parent scope.</i>         </div>         <h2>Expression</h2>             <div>             <input ng-model="isolatedFoo">             <button class="btn" ng-click="isolatedExpressionFoo({newFoo:isolatedFoo})">Submit</button>             <i>// And this calls a function on the parent scope.</i>         </div>     </my-component> </div> 
var myModule = angular.module('myModule', [])     .directive('myComponent', function () {         return {             restrict:'E',             scope:{                 /* NOTE: Normally I would set my attributes and bindings                 to be the same name but I wanted to delineate between                 parent and isolated scope. */                                 isolatedAttributeFoo:'@attributeFoo',                 isolatedBindingFoo:'=bindingFoo',                 isolatedExpressionFoo:'&'             }                 };     })     .controller('MyCtrl', ['$scope', function ($scope) {         $scope.foo = 'Hello!';         $scope.updateFoo = function (newFoo) {             $scope.foo = newFoo;         }     }]); 
like image 126
maxisam Avatar answered Sep 22 '22 23:09

maxisam