1

I often use ngRepeat to add multiple instances of a directive. For example,

<div ng-repeat="obj in list">
  <div my-directive></div>
</div>

Inside of myDirective, obj is available via the scope. Then in the directive's template I can display some data.

<div>{{obj.someText}}</div>

This works great until I want to use that directive in multiple places. Then I realize that I've implicitly defined an interface for myDirective: the data must be passed into the scope as an object named obj. It doesn't feel right. It feels accidental. It's not clearly documented.

Is there any way around this?

The only way to avoid this that I could think of is worse. It would be to iterate over the keys in the scope and looking for values which don't start with $. But if you find more than one key then you're hosed.

Perhaps my entire approach for passing data is just wrong.

  • 2
    have you tried isolating the directive scope?. Also please share your directive code – scniro Jan 14 '15 at 16:41
  • See http://stackoverflow.com/a/12372494/1522169 – noj Jan 14 '15 at 16:45
  • Read the [guide about directives](https://docs.angularjs.org/guide/directive) - it will help you understand how directives are used and defined, and the issues of not using isolate scope. – New Dev Jan 14 '15 at 16:49
  • Read [https://docs.angularjs.org/guide/directive#isolating-the-scope-of-a-directive](https://docs.angularjs.org/guide/directive#isolating-the-scope-of-a-directive) – Boris Charpentier Jan 14 '15 at 18:06

0 Answers0