Pass a function from view/controller to directive

267 views
Skip to first unread message

jock...@hotmail.com

unread,
Apr 20, 2013, 6:39:12 PM4/20/13
to ang...@googlegroups.com
Heya!


So I wrote myself two directives one for drag and one for drop.

They both work brilliant, but I got one issue.

Heres my code I wrote for drop, commented what I want to do.


As you can see I want to be able to pass a function to the directive "drop"

And when an item is dropped I want to call the function that I dont know anything about and pass the object and then handle it in the controller. 

I haven't found any answer for this yet, Is this even possible to do?


Thanks!

/Joachim


yahya Kacem

unread,
Apr 21, 2013, 1:17:13 AM4/21/13
to ang...@googlegroups.com
Hi, you use the isolate scope and the ampersand (&) not the equal sign for function, but that when you want to use the function in your directive's template not the linking function for that you need to use the attr injected to the linking function so instead of this:

    $scope.$apply( function () {
        $scope.addToTodos( data ); // this is what im doing atm
        $scope.dropFunction( data );// this is what i actually want to do
     } );

Do something like this:

    $scope.$apply(attr.drop);
this is from this videos:
I don't know if how to pass data to it, maybe this will point you to the right direction.

jock...@hotmail.com

unread,
Apr 21, 2013, 6:54:49 AM4/21/13
to ang...@googlegroups.com
Hi,

Thanks alot for your response, I'm one step closer now, only thing i want to do is to be able to send some data to my function

scope: {
            items: '=items',
            drop: '&'
}

scope.drop( data );

 scope.$apply( function () {
                    
         scope.drop( data );
                   
 } );

these both calls to drop works, but ones the function linked to drop is called the data is undefied.

Is there anyway to pass a pram to drop?

yahya Kacem

unread,
Apr 21, 2013, 7:07:04 AM4/21/13
to ang...@googlegroups.com
Maybe something like this(not tested):

    scope.drop({data: data});
and in the HTML:

    <div class="drag-and-drop" drop="addToDoings(data)"><!--This is were i want to pass a function thats bound to the controller-->
Hope this help.

On Saturday, April 20, 2013 11:39:12 PM UTC+1, jock...@hotmail.com wrote:

jock...@hotmail.com

unread,
Apr 21, 2013, 4:16:20 PM4/21/13
to ang...@googlegroups.com
Thanks alot man!

That was the solution for me, saved me loads of time.

I put my directives on git if you want to see the end result :)

Reply all
Reply to author
Forward
0 new messages