指令内的角度调用功能

时间:2015-04-01 08:40:58

标签: angularjs angularjs-directive

  .directive('optionFoundAddressClickableDisplay', function(search,$state,Service,$log) {
    'use strict';
    return {
      replace: true,
      restrict: 'AE',
      template: '<ul class="dropdown-menu apartment-group" ng-show="barClickable"><li><a href ng-click="searchMatch()"><span class="gr-arrow pull-right"></span>' +
      '<div class="result-text ng-binding"><img src="images/map-pin.png"  class="map-pin" alt=""/>{{ buildingName }}</div> </a>' +
      '</li></ul>',
      link: function(scope, element, attrs) {
        scope.buildingName = search.result.buildingAddress;
        scope.barClickable = (search.result.matchedNua || search.result.matchedId !== null) ? true : false;
        scope.searchMatch = function(){
          $log.warn("gets here");
        };
      }
    };
  })

似乎无法在我的指令中点击ng-click - searchMatch()。我是否需要以不同的方式在指令中声明函数?

1 个答案:

答案 0 :(得分:0)

在指令控制器中定义它,

EX:

.directive('optionFoundAddressClickableDisplay', function(search,$state,Service,$log) {
'use strict';
    return {
        replace: true,
        restrict: 'AE',
        template: '<ul class="dropdown-menu apartment-group" ng-show="barClickable"><li><a href ng-click="searchMatch()"><span class="gr-arrow pull-right"></span>' +
  '<div class="result-text ng-binding"><img src="images/map-pin.png"  class="map-pin" alt=""/>{{ buildingName }}</div> </a>' +
  '</li></ul>',
         link: function(scope, element, attrs) {
                   scope.buildingName = search.result.buildingAddress;
                   scope.barClickable = (search.result.matchedNua || search.result.matchedId !== null) ? true : false;
         },

         controller : function($scope) {
             $scope.searchMatch = function(){
             $log.warn("gets here");
        };
    }

 };
})

这是一个demo plunker