猿问

angularjs如何在directive里调用controller的function

请问如何在directive里面调用controller的function?代码如下:

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


    app.controller('myCtrl',function($scope){

       $scope.test= function(){

        ...

        }

    }

    app.directive('myDir',

    function(){

        return {

            restrict:'EA',

            replace: true,

            template: '<input ng-click="test()"></input>',

            scope:{

                test: '&'

            },

            controller: [

                '$scope', function ($scope) {

                    $scope.accept = function () {

                  

                       $scope.test();                       


                    };

                }

                ],

        }

    }

);

html是这样的:<button type="button" ng-click="accept()">test</button>

要怎样才能实现点击test button的时候在调用了directive里的accept function之后同时调用controller里面的test function呢?


阿波罗的战车
浏览 496回答 1
1回答

拉风的咖菲猫

&nbsp; &nbsp; var app = angular.module('myApp',[]);&nbsp; &nbsp; app.controller('myCtrl',function($scope){&nbsp; &nbsp; &nbsp; &nbsp;$scope.accept= function(){&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; app.directive('myDir',&nbsp; &nbsp; function(){&nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; restrict:'EA',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; replace: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template: '<input ng-click="test()"></input>',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scope:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test: '&'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link: function(scope, element, attrs) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 元素.bind("click",function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scope.accept();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });你看看可不可以这样子弄
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答