0

My directive works fine, but I'd like to use it in ng-click However, the function inside link can't be triggered.

http://jsfiddle.net/ovzyro1f/

<div ng-app="editer" ng-controller="myCtrl" class="container">
  <div class="parents">
    <div ng-repeat="item in items" class="wrap" sibs>
      <span>{{item.name}}</span>
      <button ng-click="">click</button>
    </div>
  </div>
</div>

JS

function myCtrl($scope) {
  $scope.editedItem = null;

  $scope.items = [{
    name: "item #1",
    thing: "thing 1"
  }, {
    name: "item #2",
    thing: "thing 2"
  }, {
    name: "item #3",
    thing: "thing 3"
  }];

  $scope.show = false; //ignore this line

}

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

editer.directive('sibs', function() {
  return {
    link: function(scope, element, attrs) {
      element.bind('click', function() {
        element.parent().children().addClass('unclicked');
        element.removeClass('unclicked');
      })

      scope.myClick = function() {
        element.parent().children().addClass('unclicked');
        element.removeClass('unclicked');
      }
    },
  }
});

I'd like to call the function in ng-click please see this one http://jsfiddle.net/ovzyro1f/2/ to remove sib from div ng-repeat="item in items" class="wrap"

 <button ng-click="myClick()">click</button> 
4
  • It works for me. I edited your JSFilddle: jsfiddle.net/ovzyro1f/1 Commented Apr 16, 2016 at 1:05
  • Yes, it is working but I'd like to call the function in ng-click please see this one jsfiddle.net/ovzyro1f/2 to remove sib from div ng-repeat="item in items" class="wrap" thanks Commented Apr 16, 2016 at 1:07
  • Why do you want to remove sibs? Commented Apr 16, 2016 at 1:11
  • I am trying to call the function in ng-click, if I only write a function like $scope.myClick = function()... it doesn't work. so I had to use directive? I am new Commented Apr 16, 2016 at 1:11

1 Answer 1

1

You should avoid to manipulate the DOM like we do in jQuery.

In Angular we think differently: it's the data which transforms automatically the DOM when the data changes (https://docs.angularjs.org/guide/databinding). Most of the time you never have to make the changes manually.

In doing so, you generally don't need to use the link function. You can have a controller (like in your example) or a directive with a controller (https://docs.angularjs.org/guide/directive).

Finally I just modified a little bit your controller and your template.

HTML

<div ng-app="editer" ng-controller="myCtrl" class="container">
  <div class="parents">
    <div ng-repeat="item in items" class="wrap" sibs>
      <span ng-class="{ unclicked: !item.selected }">{{ item.name }}</span>
      <button ng-click="selectItem(item)">click</button>
    </div>
  </div>
</div>

JS

function myCtrl($scope) {

  $scope.items = [...];

  $scope.selectItem = function (item) {

      // reset all the items
      $scope.items.forEach(function (i) {
          i.selected = false;
      });

      // set the new selected item
      item.selected = true;
  }

}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks I know I can do this way, I am trying to traverse all siblings, I have tried this way.
I edited my answer with some explanations. Hope it will help you. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.