JavaScript AngularJS call method from an ancestor scope inside directive angularjs call method on child component,angularjs call method from

I have an Angular app where I'm using ui-grid. I want to have a custom action on a cell of the grid that calls a method from my app. So basically, this means calling a method that's somewhere up in the parent hierarchy, from a directive.

This would be achieved by calling something like: $scope.$parent.$parent.$parent.$parent.foo(). But that doesn't seem too nice.

One option would be to create a recursive function that goes up the ancestry of the $scope. That's nicer, but still seems a bit weird.

Also... Is it good practice to try to achieve something like this?

Answer:1

You're correct that $parent.$parent.$parent is definitely not a good practice.

If the method you're calling is another directive, you can require that directive in your child directive and then, the parentDirective's controller function will be injected as the fourth parameter to your link function:

In your DDO:

return {
    require : '^parentDirective',
    restrict : 'E',
    link : function (scope, elem, attrs, parentDirectiveController) {}
}

If what you're trying to call is on a factory/service, you can inject that factory/service into your directive, although this sometimes is a code smell, depending on what you're trying to inject.

Finally, another way to do it is to use event propagation. From your directive, you can use $scope.$emit to send information up to parent controllers:

From the directive:

$scope.$emit('directiveDidStuff', {
    data : 'blah'
});

In the parent controller:

$scope.$on('directiveDidStuff', function (evt, params) {
    this.data = params.data;  // equals blah
});
Answer:2

I'm working with a form and would like to add ng-model on dynamic input elements. I have a scope variable defined as: $scope.formData = {}; On the page there are a couple of drop-down lists that ...

I'm working with a form and would like to add ng-model on dynamic input elements. I have a scope variable defined as: $scope.formData = {}; On the page there are a couple of drop-down lists that ...

  1. angularjs dynamic model name
  2. angularjs dynamic model value
  3. angularjs dynamic ng-model inside ng-repeat
  4. angularjs dynamic ng-model
  5. angularjs dynamic ng-model name
  6. angularjs get dynamic model value
  7. angularjs ng-model dynamic variable name
  8. angularjs ng-repeat dynamic model
  9. angularjs dynamic binding ng-model
  10. angularjs ng-model dynamic value
  11. angularjs dynamic input ng-model
  12. angularjs dynamic form ng-model

I want to pass a javascript variable to my servlet, where I need to use it. In javascript, the variable count returns the rows of my table and I can show count in the jsp, using $('#counter').html(...

I want to pass a javascript variable to my servlet, where I need to use it. In javascript, the variable count returns the rows of my table and I can show count in the jsp, using $('#counter').html(...

  1. access javascript variable in html
  2. access javascript variable in php
  3. access javascript variable in angular component
  4. access javascript variable in jsp
  5. access javascript variable in visualforce page
  6. access javascript variable from another file
  7. access javascript variable in typescript
  8. access javascript variable inside ajax success
  9. access javascript variable in c#
  10. access javascript variable in django template
  11. access javascript variable in java
  12. access javascript variable in css
  13. access javascript variable in python
  14. access javascript variable in mvc view
  15. access javascript variable in scriptlet
  16. access javascript variable in console
  17. access javascript variable in ejs
  18. access javascript variable in scss
  19. access javascript variable in jquery
  20. access javascript variable in classic asp

Can I set the biggest width and height without video cutting and page scrolling? <style> video { width: ?; height: ?; } </style>

Can I set the biggest width and height without video cutting and page scrolling? <style> video { width: ?; height: ?; } </style>

  1. 100 width height iframe
  2. 100 width height background image
  3. 100 width height div
  4. width 100 height auto
  5. width 100 height auto react native
  6. width 100 height proportional
  7. width 100 height
  8. width 100 height auto not working
  9. width 100 height square
  10. width 100 height 100vh
  11. 100(width) * 120(height) pixel
  12. textarea 100 width height
  13. 100 width and height css
  14. 100(width) * 120(height) pixels online
  15. svg 100 width height
  16. iframe width 100 height auto
  17. img width 100 height auto
  18. max-width 100 height auto
  19. background width 100 height auto
  20. image width 100 height auto

I need to get the html of a p tag, however there is a br tag inside of it. So it looks like: <p> Some Text <br> Some More Text </p> I need to get the first part and ...

I need to get the html of a p tag, however there is a br tag inside of it. So it looks like: <p> Some Text <br> Some More Text </p> I need to get the first part and ...

  1. jquery replace part of innerhtml