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

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

angularjs如何在directive里調用controller的function?

angularjs如何在directive里調用controller的function?

守著星空守著你 2019-02-05 15:07:30
請問如何在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呢?
查看完整描述

3 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

    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();

                })

            }

        }

    }

);


查看完整回答
反對 回復 2019-03-17
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

可以把myCtrl里的test function 存到service里,然后在directive里調用service里的test function

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

   $scope.test= function(){

    ...

    }

   methods.test = $scope.test;

}

app.factory('methods',function(){

        return {

            test: null

        };

})


app.directive('myDir',

    function(){

        return {

            restrict:'EA',

            replace: true,

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

            scope:{

                

            },

            controller: function ($scope,methods) {

                    $scope.accept = function () {

                       console.log('accept fn');

                       methods.test();

                    };

                }

        }

    }

);

或者:

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: function(){}

    }

});


HTML:

<my-dir test="test()"></my-dir>



查看完整回答
反對 回復 2019-03-17
?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

<button type="button" ng-click="accept()">test</button> 這是 html 里的調用方式?
然后這個是 directie 的 template: <input ng-click="test()"></input>?

所以你的 myDir 這個 directive 到底是在哪兒調用的?另外,如果方便,請你大概描述一下你要實現什么功能。


查看完整回答
反對 回復 2019-03-17
  • 3 回答
  • 0 關注
  • 1066 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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