javascript - 请问angular在controller中怎么利用service或者factory和directive通信?
ringa_lee
ringa_lee 2017-04-11 12:45:50
[JavaScript讨论组]

下面是我知道的几种方式,感觉不太对,希望大家可以帮忙看看。

1.通过事件(在线示例 http://runjs.cn/code/577rleyc)



    
    


    

Hello Angular

2.通过绑定v1(在线示例 http://runjs.cn/code/6kpq2lfb)



    
    


    

Hello Angular

3.通过绑定v2(在线示例 http://runjs.cn/code/uthyayjp)



    
    


    

Hello Angular

上面是我理解的三种通过angular的service,factory和directive交互的方式。
a.对于第一种方式,总觉得太简单,没有作用域,声明多个指令时看到会乱套
b.对于第二种方式,还是觉得太简单,虽然可以多实例,但是遇到构建table等重量级angular directive肯定不行
c.对于第三种方式,觉得应该是比较好的方式,但是总觉得directive中的方法都要在service中声明一遍,重复,冗余,不理想。

我理解的第三种方式,是很强大的可以让directive向外提供api,传递配置等。我看ng-table有点类似这样的方式。
eg:

angular
    .module('app', [])
    .service('MyColor', function () {
        return function (opts) {
            var options = {};
            angular.extend(options, opts);


            this.controller = null;
            this.bindToController = function (ctrl) {
                this.controller = ctrl;
            };
            this.changeColor = function (color) {
                this.controller.changeColor(color);
            };
            this.getBackColor = function () {
                return this.controller.getBackColor();
            };
            this.method1 = function () {
                this.controller.method1();
            };
            this.method2 = function () {
                this.controller.method2();
            };
            this.methodN = function () {
                this.controller.methodN();
            }
        }
    })
    .directive('myColor', function () {
        return {
            scope: {
                color: '=myColor'
            },
            controller: function ($scope, $element) {
                this.color = $scope.color;
                this.color.bindToController(this);
                this.changeColor = function (color) {
                    $element.css('background-color', color);
                };
                this.getBackColor = function () {
                    return $element.css('background-color');
                };
                this.method1 = function () {
                    console.log('method1');
                };
                this.method2 = function () {
                    console.log('method2');
                };
                this.methodN = function () {
                    console.log('methodN');
                }
            }
        }
    })
    .controller('TestController', function (MyColor) {
        this.color = new MyColor();
        this.changeColor = function (color) {
            this.color.changeColor(color);
        };
        //可以调用对外的方法
        this.color.getBackColor();
        this.color.method1();
        this.color.method2();
        this.color.methodN();
    })

下面看下ng-table的使用方式



    
{{row.name}} {{row.money}}

大家帮忙看看有没有好的angular中通过factory、service调用directive的方式。或者说,directive通过什么方式提供api。

ringa_lee
ringa_lee

ringa_lee

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

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