How to check in an Angular directive if the parent Node is a link?

Asked

Viewed 135 times

1

I need to make a check if my directive xx-chip is within a link. I cannot use jQuery in this application, only Angular.

<a href>
    <xx-chip></xx-chip>
</a>

Thank you.

  • It would be very quiet to use .is(), but unfortunately I do not have this resource.

2 answers

0


After an analysis and some console.log in the code, I was able to find the solution I was so eager to find:

angular
    .module('xx.chip', [])
    .directive('xxChip', chip);

function chip() {
    var directive = {
        ...
    };

    return directive;

    function link(scope, element) {
        // Remove o underline do elemento pai se for um link
        if (element.parent()[0].localName === 'a') {
            element.parent().css('text-decoration', 'none');
        }
    }
}

I hope you help other developers.

0

I think this is the best answer, will help you solve, it’s exactly what you want only using the most usual expression (father and son), as we conventionally use (SO-Eng):

HTML

<div ng-app ng-controller="ParentCtrl as pc">
    <div ng-controller="ChildCtrl as cc">
        <pre>{{cc.parentCities | json}}</pre>
        <pre>{{pc.cities | json}}</pre>
    </div>
</div>
JS

function ParentCtrl() {
    var vm = this;
    vm.cities = ["NY", "Amsterdam", "Barcelona"];
}

function ChildCtrl() {
    var vm = this;
    ParentCtrl.apply(vm, arguments); // Inherit parent control

    vm.parentCities = vm.cities;
}

If you use the controller as a method you can also access the parent scope as follows:

function ChildCtrl($scope) {
    var vm = this;
    vm.parentCities = $scope.pc.cities; //Note que PC é uma referência para o "ParentCtrl como pc"
}

As you can see, there are many different ways to access '$Scope' scopes'.

Fiddle:

function ParentCtrl() {
    var vm = this;
    vm.cities = ["NY", "Amsterdam", "Barcelona"];
}

function ChildCtrl($scope) {
    var vm = this;
    ParentCtrl.apply(vm, arguments);

    vm.parentCitiesByScope = $scope.pc.cities;
    vm.parentCities = vm.cities;
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
<div ng-app ng-controller="ParentCtrl as pc">
  <div ng-controller="ChildCtrl as cc">
    <pre>{{cc.parentCities | json}}</pre>
    <pre>{{cc.parentCitiesByScope | json }}</pre>
    <pre>{{pc.cities | json}}</pre>
  </div>
</div>

Original: https://stackoverflow.com/questions/21453697/angularjs-access-parent-scope-from-child-controller

  • I probably couldn’t explain it better, I just need to check if the parent element is a link (a).

  • 1

    Boy, in this case pure javascript solves no ? Define an attribute name='xx-chip' for the tag 'xx-chip' <xx-chip name='chip'></xx-chip>, and then give a name to <a href name='href'></a> and do this: var x = Document.getElementById("xx-chip").parentElement.nodename; .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.