加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angularjs服务是单例吗?

发布时间:2020-12-17 08:29:42 所属栏目:安全 来源:网络整理
导读:在 referenceI读: Lastly,it is important to realize that all Angular services are application singletons. This means that there is only one instance of a given service per injector. 但是用这个简单的代码似乎不是一个单例 'use strict'; angula
在 referenceI读:

Lastly,it is important to realize that all Angular services are
application singletons. This means that there is only one instance of
a given service per injector.

但是用这个简单的代码似乎不是一个单例

'use strict';
            angular.module('animal',[])
                .factory('Animal',function(){
                    return function(vocalization){
                        return {
                            vocalization:vocalization,vocalize : function () {
                                console.log('vocalize: ' + this.vocalization);
                            }
                        }
                    }
                });    
                angular.module('app',['animal'])
                    .factory('Dog',function (Animal) {
                        return Animal('bark bark!');
                    })
                    .factory('Cat',function (Animal) {
                        return Animal('meeeooooow');
                    })
                .controller('MainCtrl',function($scope,Cat,Dog){
                     $scope.cat = Cat;
                     $scope.dog = Dog;
                     console.log($scope.cat);
                     console.log($scope.dog);
                    //$scope.cat = Cat;
                });

我有点困惑你能解释我什么事?

更新1
可能我不是在棚子里最锋利的工具
但是在@Khanh TO回复它会是一个更好
解释在参考它不是很清楚。

更新2

'use strict';
            angular.module('animal',function(){
                        return {
                            vocalization:'',vocalize : function () {
                                console.log('vocalize: ' + this.vocalization);
                            }
                        }

                });
                angular.module('dog',function (Animal) {
                        Animal.vocalization = 'bark bark!';
                        Animal.color = 'red';
                        return Animal;
                    });

                angular.module('cat',['animal'])
                   .factory('Cat',function (Animal) {
                        Animal.vocalization = 'meowwww';
                        Animal.color = 'white';
                        return Animal;
                    });
                 angular.module('app',['dog','cat'])
                .controller('MainCtrl',Dog){
                     $scope.cat = Cat;
                     $scope.dog = Dog;
                     console.log($scope.cat);
                     console.log($scope.dog);
                    //$scope.cat = Cat;
                });

BOOM这是一个单身!

更新3

但如果你喜欢

'use strict';
            angular.module('animal',function (Animal) {
                        function ngDog(){
                            this.prop = 'my prop 1';
                            this.myMethod = function(){
                                console.log('test 1');
                            }
                        }
                        return angular.extend(Animal('bark bark!'),new ngDog());
                    })
                    .factory('Cat',function (Animal) {
                        function ngCat(){
                            this.prop = 'my prop 2';
                            this.myMethod = function(){
                                console.log('test 2');
                            }
                        }
                        return angular.extend(Animal('meooow'),new ngCat());
                    })
                .controller('MainCtrl',Dog){
                     $scope.cat = Cat;
                     $scope.dog = Dog;
                     console.log($scope.cat);
                     console.log($scope.dog);
                    //$scope.cat = Cat;
                });

有用

它是单例,只有一个对象,但是注入到许多地方。 (通过引用方法传递对象)

所有你的Animal都是指向同一个动物对象的对象指针,这是你的情况下的一个函数。您的Cat和Dog是由此函数构建的对象。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读