最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJs: multiple directives asking for isolated scope on - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jan 12, 2014 at 10:25 Rustam asked Jan 12, 2014 at 9:50 RustamRustam 1531 gold badge1 silver badge7 bronze badges 3
  • Look here why you get this error docs.angularjs.org/error/… – Tasnim Reza Commented Jan 12, 2014 at 10:05
  • How do I get the attribute values ​​without scope? – Rustam Commented Jan 12, 2014 at 10:12
  • 2 You could use the Attrs(third) argument in your linking function and then $eval or $parse it against your scope in the function's body. – Kia Raad Commented Jan 12, 2014 at 10:33
Add a comment  | 

1 Answer 1

Reset to default 17

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

    }
}
})
发布评论

评论列表(0)

  1. 暂无评论