Binding directive from another directive

3,199 views
Skip to first unread message

Pratheep Velicherla

unread,
Dec 29, 2011, 3:39:24 AM12/29/11
to ang...@googlegroups.com
Hi,

I am new to angular js. I tried to extend the directives. I have created two directives and i want to attach one directive dynamically from another. Say D1 in thins linkfunction i need to bind D2 based on some specific condition. Is there any option to do that.?

Vojta Jina

unread,
Dec 29, 2011, 5:22:24 PM12/29/11
to ang...@googlegroups.com
Hey, can you explain bit more, what you are trying to achieve ? Posting jsfiddle might help... http://jsfiddle.net/vojtajina/YdAsa/

You can apply directive from directive, but not sure why would you do that. Something like this should work:

angular.directive('first', function(expression, element) {
  var compiler = this;
  // compile....
  return function(instance) {
    var scope = this;
    // link...
  };
});

angular.directive('second', function(expression, element) {
  // apply compile fn of first directive
  var firstLinker = angular.directive('first').call(this, 'some-expression', element);

  return function(instance) {
    // apply linker of the first directive
    firstLinker.call(this, instance);
  };
});

V.

Witold Szczerba

unread,
Jan 3, 2012, 5:45:06 PM1/3/12
to ang...@googlegroups.com
Hi Vojta,
I was thinking about being able reusing/wrapping directives inside one
another. For example: there is ng:repeat, but I would like to create
my own ng:repeat which would do exactly the same plus something extra
at the end. Is it possible using what you have shown? Also, the
reality is the current directives/widgets API is going to be replaced
soon. I was browsing pull request made by Misko. Do you think that
what you have just shown would be possible in the future?

Thanks,
Witold Szczerba

> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/lBXIeEwOdGEJ.
>
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/angular?hl=en.

Vojta Jina

unread,
Jan 5, 2012, 10:14:30 PM1/5/12
to ang...@googlegroups.com
Hi Witold, sorry for delay, I was kinda not sure what to respond :-D

To apply directive from within another directive(the code I sent in previous post), you only need to get hold of the directive, so it should be possible, but we would rather prefer to use composition, basically something like this:
<li ng:repeat="i in items" my-custom="whatever"></li>

Where "my-custom" is directly that might extend behavior of ng:repeat.

Can you share any concrete things you would like to achieve ?

V.

Dalci de Jesus Bagolin

unread,
Jan 7, 2012, 2:58:14 PM1/7/12
to ang...@googlegroups.com
I am trying to build a Grid Widget over HTML Table and Repeat Widget, to make the code more declarative.
This approach allow to insert things like Pagination, Order etc.

I tried in many ways, but always is returned the one row with expressions {{..}} without the values.
I think that is need to compile de widget ng:repeat inside de ng:grid to work.
I tried to apply de function angular.widget("@ng:repeat", "a in A", element).call ... but also don't worked.

My last tentative is here: http://jsfiddle.net/dalcib/2Cznj/2/

NameEstimateDoneCreate At
{{todo.name}}{{todo.estimate | number}}{{todo.done }}{{todo.created_at }}

Vojta Jina

unread,
Jan 8, 2012, 2:52:29 AM1/8/12
to ang...@googlegroups.com
Hey Dalci,

here is your fiddle fixed: http://jsfiddle.net/vojtajina/2Cznj/3/

V.

Dalci de Jesus Bagolin

unread,
Jan 8, 2012, 10:39:28 AM1/8/12
to ang...@googlegroups.com
Thanks Vojta. This is all I need.

Dalci

Vojta Jina

unread,
Jan 8, 2012, 7:15:14 PM1/8/12
to ang...@googlegroups.com
Great, I'm glad it helped you.

V.

Matthias Andrasch

unread,
Jan 25, 2012, 3:15:46 PM1/25/12
to ang...@googlegroups.com
I like this approach. If you have an example with implemented pagination or order-methods, it would be really nice if you share it! :)

Marco Alves

unread,
Jan 25, 2012, 3:45:27 PM1/25/12
to ang...@googlegroups.com
Regarding grids, is it possible to create a widget like the one bellow?

<grid>
r0c0 r0c1
r1c0 r1c1
</grid>

Where rxcy is the element in row x, col y.

I've tried to do it, but I don't know how to get the inner contents between the tags.

Dalci de Jesus Bagolin

unread,
Jan 25, 2012, 8:34:29 PM1/25/12
to ang...@googlegroups.com
My Grid widget with Order and Pagination is here: 


The goal this widget is make the code more declarative.

It is not ready.
The grid only order and page for local Array. 
The order for numbers don't work well.

I appreciate sugestions to improve the code.

Dalci

Vojta Jina

unread,
Jan 25, 2012, 11:30:31 PM1/25/12
to ang...@googlegroups.com
Nice grid man !

V.

Misko Hevery

unread,
Jan 25, 2012, 11:43:42 PM1/25/12
to ang...@googlegroups.com
ok, I am impressed.

On Wed, Jan 25, 2012 at 8:30 PM, Vojta Jina <vojta...@gmail.com> wrote:
Nice grid man !

V.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/EepFMwOCYpgJ.

n...@posse.com

unread,
Mar 23, 2012, 1:29:42 AM3/23/12
to ang...@googlegroups.com
What is the syntax to do this in angular 1.0rc2?

Igor Minar

unread,
Mar 26, 2012, 1:09:38 PM3/26/12
to ang...@googlegroups.com
This is possible with rc2. A directive in 1.0.0rc1+ can't decorate another directive.

/i


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/G9Sqj2RDSPYJ.

Igor Minar

unread,
Mar 26, 2012, 1:10:30 PM3/26/12
to ang...@googlegroups.com
With all the new directive features we'd have to teach the compiler how to do directive decoration.

/i

Tim Sweet

unread,
Nov 4, 2012, 12:58:43 AM11/4/12
to ang...@googlegroups.com
as fyi, ng-grid project https://github.com/timothyswt/ng-grid
Reply all
Reply to author
Forward
0 new messages