使用AngularJS,当元素是一个已被排除的指令的克隆时,如何赋予它自己的作用域?
例如,说我有以下的HTML:
<body>
<chicken></chicken>
</body>以及以下指令:
app.directive('chicken', [
'$rootElement',
function($rootElement) {
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<div class="chicken" ng-transclude></div>',
scope: {},
link: function(scope, element, attrs) {
scope.nature = 'timid';
var egg = element.clone();
$rootElement.append(egg);
}
};
}
]);如何确保egg有它自己的范围?
发布于 2014-06-17 09:19:52
创建一个新的范围并将其链接起来:
link: function(scope, element, attrs) {
scope.nature = 'timid';
var egg = element.clone();
var childScope = $rootElement.scope().new();
$compile(egg)(childScope);
$rootElement.append(egg);
}https://stackoverflow.com/questions/24259535
复制相似问题