feature request: specify directive/attribute name using scope

23 views
Skip to first unread message

John Grant

unread,
Jun 27, 2015, 8:23:36 PM6/27/15
to ang...@googlegroups.com
I'd like to be able to do this:
<div ng-attr-{{obj.name}}="{{obj.val}} />

as well as
<div ng-attr-{{obj.name}} />

and possibly even this:
<{{obj.name}} />


My understanding is that this is not currently possible. I'm using v1.3.16.

A common pattern for me is to leverage directives, and to use different directives to create a custom look when my data has different values in it. I am currently writing giant ng-switch permutations to use the desired directive. I want to specify the name of the directive with my data.

Since 'ng-attr-cx' can be used to produce the 'cx' dom attribute, I feel like this is within reach.

Thanks for any help understanding the issues.

Rob R

unread,
Jun 28, 2015, 9:20:20 AM6/28/15
to ang...@googlegroups.com
Not sure how your switch statement plans on being implemented but couldn't you do this:

<directive-name obj-name="{{obj.name}"  obj-val="{{obj.val}}"></directive-name>

angular.directive('directiveName',directiveName);

function directiveName() {
  var directive = {
    restrict:'E',
    templateUrl: 'directive-name.html',
    scope: {
    objName: '@objName',
    objVal: '@objVal'
    }
  }

  return directive;
}

Then put the switch in the directive-name.html and use scope.objName and scope.objVal?

John Grant

unread,
Jun 28, 2015, 2:27:42 PM6/28/15
to ang...@googlegroups.com

Hi Rob,

I suspect the point of my message did not make it over the line.

I see your directive definition, and your usage of the directive, but I do not see the contents of your directive's template.

The contents is the point of my request. Angular does not support the feature needex to use objName as an attribute identifier.

Does that make sense?

Thanks for the discussion.
John


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/rNmI6CxEyUI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Rob R

unread,
Jun 29, 2015, 10:13:30 AM6/29/15
to ang...@googlegroups.com
I was just trying to provide a directive example, not create the template.  If you provide a more detailed example of what you're trying to accomplish then I might be able to help.

Alain Chautard

unread,
Jun 29, 2015, 1:24:46 PM6/29/15
to ang...@googlegroups.com
"Custom look when my data has different values in it" => Then I would definitely implement that using CSS with conditional styles.

John Grant

unread,
Jun 29, 2015, 2:06:36 PM6/29/15
to ang...@googlegroups.com
Hi guys,

This is a feature request. Perhaps this is the wrong forum to propose such things.

My first post showed exactly what I want to do.

<div ng-attr-{{obj.name}}="{{obj.val}} />

<div ng-attr-{{obj.name}} />

<{{obj.name}} />


I realized another use case is:
<div {{obj.name}} />


CSS cannot achieve the diversity of content and use of completely different directives.

I will look for an alternate forum.

Thanks,
John



Reply all
Reply to author
Forward
0 new messages