javascript AngularJs:多个指令要求隔离范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21073295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:58:58  来源:igfitidea点击:

AngularJs: multiple directives asking for isolated scope on

javascriptangularjs

提问by Rustam

I created two directives:

我创建了两个指令:

directivesModule.directive("capital", function () {
return {
    scope: {
        capital: "@"
    },
    link: function () {}
}
})

directivesModule.directive("country", function () {
return {
    scope: {
        country: "@"
    },
    link: function () {}
}
})

Next, I use them in the same element:

接下来,我在同一个元素中使用它们:

<div country="Russia" capital="Moscow"></div>

As a result, I get an error: Error: [$compile:multidir] Multiple directives [capital, country] asking for new/isolated scope on: <div country="Russia" capital="Moscow">

How do I get the attribute values without scope? These directives will not necessarily be used in conjunction.

结果,我收到一个错误:Error: [$compile:multidir] Multiple directives [capital, country] asking for new/isolated scope on: <div country="Russia" capital="Moscow">

如何在没有作用域的情况下获取属性值?这些指令不一定要结合使用。

回答by Daiwei

According to your code, you don't need isolated scope to get attribute value. Just use this:

根据您的代码,您不需要隔离范围来获取属性值。只需使用这个:

directivesModule.directive("capital", function ($parse) {
return {
    link: function (scope, element, attrs) {

      // get attrs value
      attrs.capital

    }
}
})