JavaScript Passing data from custom directive into parent controller passing data from child to parent react,passing data from

I have created custom angular directive. For an example:

Custom directive:

var app=angular.module('app',[]);
app.directive('customDirective',function(){
return{
   restrict:A,
   controller:customDirectiveController,
   scope:{
         someArray:"="
         }
   }
})

Custom directive controller:

app.controller('customDirectiveController',function(scope){
    scope.someArray=[];
    scope.someArray.push(1);
    scope.someArray.push(2);
    scope.someArray.push(3);
});

Parent controller:

app.controller('parentCtrl',function($scope){
    $scope.result=[];
});

HTML:

<div data-ng-controller="parentCtrl">
  <div data-custom-directive="result">
</div>

How can I get value of this someArray from custom directive into Parent controller( result variable should in Parent controller be same as someArray from custom directive controller)?

Here is jsfiddle http://jsfiddle.net/mehmedju/RmDuw/302/ Thanks

Answer:1

Let's just say you are using tempController So your code should be

app.controller('tempController', function($scope) {
    scope.someArray = []
});

Html Code for this is

<div ng-controller="tempController">
   <div custom-directive some-array="someArray">
</div>
Answer:2

You have here a very interesting article about watchers http://teropa.info/blog/2014/01/26/the-three-watch-depths-of-angularjs.html

You need use a watchCollection

And, If you are playing creating the array and change the reference inside the directive you can lost the reference inside the watcher. This mean don't create o change the array reference inside.

Another interesting way is use ngModel http://jsfiddle.net/ta66J/

var app = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.formVals = {
        dirVals: [
            {val: 'one'},
            {val: 'two'}
        ]
    };
}

app.directive('dir', function($compile) {
    return {
        restrict: 'E',
        compile: function(element, attrs) {
            var html = "<input id='inputId' type='text' ng-model='" + attrs.dirModel + "' />";
            element.replaceWith(html);
            return function(scope, element, attrs, ngModel) {
                $compile(angular.element(element))(scope);
            };
        },
    };
});

The jsfidler is from this interesting thread: https://groups.google.com/forum/#!topic/angular/QgcRBpjiHAQ

Answer:3

You can apply a '$watch' on the array like this:

In the controller:

app.controller('MainCtrl', function($scope) {
    $scope.someArray = [];
})

In the HTML:

<div custom-directive arr="someArray">
</div>

In the directive:

app.directive('customDirective', function(){
    return {
        scope: {
           arr: '='
        }
    }
    link: function(scope, element, attrs) {
        scope.$watch('arr', function(newVal, oldVal) {
            //do your array manipulation here
        }
    }
})

Alternatively, if you just want to send data back, here's the method:

In the controller, create a function which will accept the value returned from the directive, example:

app.controller('MainCtrl', function(){
    $scope.watchVal = function(val) {
        //do array manipulation
        $scope.apply(); //to update the scope
    }
})

In the HTML:

<div custom-directive data-method="watchVal">
</div>

In the directive:

app.directive('customDirective', function(){
    return {
        scope: {
            sendVal: '&method'
        },
        link: function(scope, element, attrs){
           scope.updateVal = function(){
               var func = scope.sendVal();
               func(scope.someArray);
           }
        }
    }
})
Answer:4

So I have something like this.editElement = $("<a href='#'>Edit</a>").click(function() { this.startEdit(); }); This however takes "this" to be the editElement itself, rather than the ...

So I have something like this.editElement = $("<a href='#'>Edit</a>").click(function() { this.startEdit(); }); This however takes "this" to be the editElement itself, rather than the ...

  1. jquery this inside function
  2. jquery this inside this
  3. jquery this inside each
  4. jquery this inside click function
  5. jquery this inside class
  6. jquery this inside if
  7. jquery this inside ajax success
  8. jquery this inside event handler
  9. jquery this inside element
  10. jquery this inside each loop
  11. jquery this inside ajax
  12. jquery this inside click
  13. jquery this inside event
  14. jquery this inside onclick
  15. jquery this inside class selector
  16. jquery access this inside function
  17. jquery inside html
  18. jquery inside php
  19. jquery inside iframe
  20. jquery inside javascript

I have a function: var foo = function() { document.write( bar() ); }; My Jasmine test is: describe('has a method, foo, that', function() { it('calls bar', function() { spyOn(window, ...

I have a function: var foo = function() { document.write( bar() ); }; My Jasmine test is: describe('has a method, foo, that', function() { it('calls bar', function() { spyOn(window, ...

I m trying to add page number in footer this is my code var pdfInfo = {}; var x = document.location.search.substring(1).split('&'); for (var i in x) { var z = x[i].split('=',2); pdfInfo[z[...

I m trying to add page number in footer this is my code var pdfInfo = {}; var x = document.location.search.substring(1).split('&'); for (var i in x) { var z = x[i].split('=',2); pdfInfo[z[...

I'm working on an Office Add-in (formerly Apps for office) using office.js library. My app adds a handler to get notified about data changes in excel sheet: Microsoft.Office.WebExtension.select("...

I'm working on an Office Add-in (formerly Apps for office) using office.js library. My app adds a handler to get notified about data changes in excel sheet: Microsoft.Office.WebExtension.select("...