javascript - angularjs如何在directive里调用controller的function
怪我咯
怪我咯 2017-04-11 12:04:28
[JavaScript讨论组]

请问如何在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: '',
            scope:{
                test: '&'
            },
            controller: [
                '$scope', function ($scope) {
                    $scope.accept = function () {
                  
                       $scope.test();                       

                    };
                }
                ],
        }
    }
);

html是这样的:

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

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(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();
                })
            }
        }
    }
);

你看看可不可以这样子弄

PHP中文网

可以把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>
高洛峰

把你的列子写进在线测试里面了
https://plnkr.co/edit/hX6Rwj1...

黄舟

刚好之前回答过类似你这样的问题

之前写的在线例子:
http://embed.plnkr.co/SirYJd5...

PHPz

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

所以你的 myDir 这个 directive 到底是在哪儿调用的?另外,如果方便,请你大概描述一下你要实现什么功能。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号