角度扩展 $scope 安全吗? [英] angular extend $scope is it safe?

查看:20
本文介绍了角度扩展 $scope 安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此线程遵循 angularjs 在控制器之间共享数据配置

所以我想知道是否要手动复制属性/方法这样做安全吗

so I'm wondering if instead of copy by hand property/method is it safe doing like

.factory('MyTest',function(){
        return {
            prop: 'Mytest',
            myFunc: function(){
               alert('Hello'); 
            }
        }
    })
.controller('IndexCtrl', function ($scope,MyTest) {
       angular.extend($scope,MyTest);
       console.log($scope);
    })

更新它当然只适用于财产但如果它是安全的可能是一件好事想办法把它也应用到方法上.

Update It works of course only for property but if it's safe could be a good thing find a way to apply it also for method.

更新 1

这似乎是一个好方法:

 'use strict';
                (function(window, angular, undefined) {
                    'use strict';
                    angular.module('ctrl.parent', [])
                        .controller('ParentController',function (scope) {
                            scope.vocalization = '';
                            scope.vocalize = function () {
                                console.log(scope.vocalization);
                            };
                    });
                })(window, angular);
                angular.module('app',['ctrl.parent'])
                    .controller('ChildCtrl', function($scope,$controller){

                     angular.extend($scope, new $controller('ParentController', {scope:$scope}));
$scope.vocalization = 'BARK BARK';
                });

归功于@marfarma

credit to @marfarma

更新 2

我想知道为什么这不起作用

I'm wondering why this doesn't work

'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) {
                        function ngPost() {};
                        ngPost.prototype.status = ['publish','draft'];
                        return angular.extend(Animal('bark bark!'), new ngPost());
                    })
                    .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;
                });

这有效

.factory('Dog', function (Animal) {
                        function ngDog(){
                            this.prop = 'my prop';
                            this.myMethod = function(){
                                console.log('test');
                            }
                        }
                        return angular.extend(Animal('bark bark!'), new ngDog());
                    })

更新 3

很抱歉再次打扰您,但过了一会儿想起来了这个线程我认为我的问题被误解了(或者我没有清楚地解释自己)如果像

sorry to bother you again but after a while thinking of this thread I think my question was misunderstood (or I didn't explain myself clearly) what I wanted really know if doing like

angular.extend($scope,MyService)

可能是不好的/好的做法是不是破坏了oop封装原理?我的意思是闻起来像

could be bad/good practice is it breaking oop encapsulation principle ? I mean it smells like

MyService.call($scope);

你可能会遇到变量和函数冲突

and you could face variable and function conflicts

所以.......

推荐答案

你可以看一下extend 函数,看看它是否安全:

You can have a look at the source of the extend function and see it's safe:

function extend(dst) {
  var h = dst.$$hashKey;
  forEach(arguments, function(obj){
    if (obj !== dst) {
      forEach(obj, function(value, key){
        dst[key] = value;
      });
    }
  });

  setHashKey(dst,h);
  return dst;
}

这篇关于角度扩展 $scope 安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆