您需要定义隔离范围以使指令可重用.一个简单的解决方法是添加范围:{}以创建一个隔离的范围,这样当您单击每个按钮时,它只会触发一次.
原文链接:https://www.f2er.com/angularjs/142118.htmlapp.directive('myDirective',function () { return { restrict: 'E',scope: {},// Add this line to create an isolated scope template: '<div>Foo: {{foo}}</div><button ng-click="incrementFoo()">Increment Foo</button>',controller: function ($scope) { $scope.foo = 0; $scope.incrementFoo = function () { $scope.foo += 1; }; $scope.$watch('foo',function () { $scope.$emit('fooChanged',{foo: $scope.foo}); console.log($scope); }); } }; });