AngularJS allows to execute expression in $parent
context with specified values, in your case doSomething()
.
Here's what you need to do that:
- In directive definition, mark
directUpload
as expression:
scope: {
directUpload: "&"
}
- In
done
callback, call:
scope.directUpload({fileInfo: fileInfo})
- Update markup:
<input type="file" direct-upload="doSomething(fileInfo)">
To summorize: scope.directUpload
is now a callback, which executes expression inside attribute with specifeid values. This way you can pass anything into controller's doSomething
.
Read $compile docs for detailed explanation and examples.
Example you might find useful:
angular
.module("app", [])
.directive("onDone", function ($timeout) {
function link (scope, el, attr) {
$timeout(function () {
scope.onDone({
value: "something"
});
}, 3000)
}
return {
link: link,
scope: {
onDone: "&"
}
}
})
.controller("ctrl", function ($scope) {
$scope.doneValue = "nothing";
$scope.done = function (value) {
$scope.doneValue = value;
};
})
<body ng-controller="ctrl">
Waiting 3000ms
<br>
<div on-done="done(value)">
Done: {{doneValue}}
</div>
</body>