亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

angularjs如何在directive里調用controller的function

angularjs如何在directive里調用controller的function

阿波羅的戰車 2018-10-12 14:15:45
請問如何在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呢?
查看完整描述

1 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

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


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

       $scope.accept= function(){

        ...

        }

    }

    app.directive('myDir',

    function(){

        return {

            restrict:'EA',

            replace: true,

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

            scope:{

                test: '&'

            },

            link: function(scope, element, attrs) {

                元素.bind("click",function(){

                    scope.accept();

                })

            }

        }

    }

);

你看看可不可以這樣子弄


查看完整回答
反對 回復 2018-11-18
  • 1 回答
  • 0 關注
  • 516 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號