0

I have a custom directive. In my html i write this:

<uploader action="/rest/file/create_from_form.json" success="writeFid()"></uploader>

What I need is to exec "success" attribute function passing some data that I get from my directive. I can exec "success" attribute function via

$scope.$eval($scope.success)

and in my "controller" I have this:

$scope.writeFid = function (data) {
        console.log("Into writeFid");
        console.log(data);  //this is my problem: it is always undefined.
    }

I can see (via console.log() messages) that "success" function is called but without passing "data".

I have tried to use

<uploader action="/rest/file/create_from_form.json" success="writeFid(data)"></uploader>

but it does not work.

So: how can i pass some type of $scope.data ?

Thanks.

2

2 Answers 2

3

I'm not sure whether your directive uses an isolate scope or not, so I'll cover both cases:

Without an isolate scope:

scope.$eval(attrs.success, { data: 'Some value from foo' });

With an isolate scope:

scope: { success: '&' },
...
scope.success({ data: 'Some value from bar' });

Regardless of the type of the scope, your markup must be like this:

<uploader success="writeFid(data)" ...></uploader>

And here's a plunk script showing both approaches.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use an isolate scope with a bi-directional binding in your uploader directive to somewhat achieve what you're after.

uploader directive

...
scope: {
    data: '=',
    success: '&'
}
...

Sample parent controller

app.controller('MainCtrl', function($scope) {
   $scope.dataAttribFromParent = { data: [] };

   $scope.writeFid = function () {
       console.log($scope.dataAttribFromParent);
   };
});

markup

...
<uploader data="dataAttribInParent" success="writeFid()">
...

Here's a plunk to elaborate:

http://plnkr.co/edit/7Kzy6D7cwxlATEBKRcuT

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.