如何在指令Angular JS中处理ng-click?

时间:2017-03-08 14:43:11

标签: angularjs

我有以下指令:

.directive("feedList", function() {
            return {
                restrict: 'E',
                scope: {
                    feeds: '=feedData'
                },
                templateUrl: 'jstemplates/feed-list.html',


                link: function(scope) {

                    angular.forEach(scope.feeds, function(value, key) {
                        if(value.who.fullname == " "){
                            scope.feeds[key].fullname = "email";
                        }
                          console.log(value.who.fullname);
                    });

                }
        }
    })

在我的模板中有一个事件:ng-click="do()"。如何在父控制器的指令中处理这个事件?

1 个答案:

答案 0 :(得分:2)

由于它是您的隔离范围指令,因此传递回调函数,然后直接从模板或控制器或链接函数调用该函数。 Working fiddle



DataGrid

var app = angular.module('myApp', []);
app.controller('AppCtrl', function($scope){
		$scope.testFunction = function(){
  			alert("Called from isolated scope directive");
  	};
});

app.directive("isolatedScopeDirective", function(){
    return{
        scope:{
         go:"&"
        },
        template : `<button ng-click='go()'>Test Button</button>`
    };
});
&#13;
&#13;
&#13;