简体   繁体   English

ng-click中的Angularjs指令链接调用函数

[英]Angularjs directive link call function in ng-click

My directive works fine, but I'd like to use it in ng-click However, the function inside link can't be triggered. 我的指令可以正常工作,但是我想在ng-click使用它。但是,链接内部的函数无法触发。

http://jsfiddle.net/ovzyro1f/ 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 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" 我想在ng-click中调用该函数,请参阅此http://jsfiddle.net/ovzyro1f/2/div ng-repeat="item in items" class="wrap"删除sib div ng-repeat="item in items" class="wrap"

 <button ng-click="myClick()">click</button> 

You should avoid to manipulate the DOM like we do in jQuery. 您应该避免像在jQuery中那样操作DOM。

In Angular we think differently: it's the data which transforms automatically the DOM when the data changes ( https://docs.angularjs.org/guide/databinding ). 在Angular中,我们有不同的看法:数据更改时,数据会自动转换DOM( 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 ). 您可以有一个控制器(如您的示例中所示)或带有控制器的指令( https://docs.angularjs.org/guide/directive )。

Finally I just modified a little bit your controller and your template. 最后,我只是稍微修改了您的控制器和模板。

HTML 的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 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;
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM