角延伸$范围是它的安全? [英] angular extend $scope is it safe?
问题描述
此线程遵循<一个href=\"http://stackoverflow.com/questions/21427027/angularjs-share-config-directives-between-controllers\">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
这似乎是一个很好的方式:
This seems to be a good way:
'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)
可能是坏/好习惯
它是空中接力打破封装原则?
我的意思是它闻起来像
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
所以.......
推荐答案
您可以看看的<一个源href=\"https://github.com/angular/angular.js/blob/7a78aed1603ca7f60f2f70ba27f0102e08862550/src/Angular.js#L325\"相对=nofollow> 延长
功能,看看它是安全的:
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;
}
这篇关于角延伸$范围是它的安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!