繁体   English   中英

角度相同的指令的多个实例,范围不是孤立的,相同的范围

[英]Angular multiple instances of same directive, scope is not isolated , same scope

我在页面上多次使用指令,例如

$scope.stylusright[0] = 330;

var appendHtml = $compile('<directive-history rightstylus="{{stylusright[0]}}"></directive-history>')($scope);

$element.append(appendHtml);

$scope.stylusright[1] = 660;

var appendHtml = $compile('<directive-history rightstylus="{{stylusright[1]}}"></directive-history>')($scope);

$element.append(appendHtml);

我的指令看起来像

angular.module('mymodule').directive('directiveHistory', function ($compile)
{
return {
restrict: 'E',
scope: true,
transclude: true,
templateUrl: 'directive.history.html',
scope: {
rightstylus: "@",
}, 
...

我的问题是,当同一指令多次添加到同一页面时,它们的作用域将变为相同。 当我在第二个指令中更改$ scope.stylusright [1]时,也会在所有其他指令中进行更改。

您的指令代码看起来没有错,(只有您有重复的作用域键)。

看一下这个punker,这与您要对独立范围指令plunker进行的操作类似

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM