How do I pass on arguments to the end()
method of the controller from the directive?
Directive
var fileuploader = function () {
return {
restrict: 'E',
scope: {
onEnd: '&',
},
controller: function ($scope) {
// When upload is done
$scope.onEnd(/* file */);
}
};
}
Controller
module.controller('Ctrl', function ($scope) {
$scope.end = function (file) {
console.log('file', file);
};
});
Template:
<div ng-controller='Ctrl'>
<fileuploader on-end='end()'></fileuploader>
</div>
I also wonder if this is the angular way of doing things because I don't see this used anywhere else. Maybe the following is more angular?
Directive
var fileuploader = function () {
return {
restrict: 'E',
scope: {
onEnd: '=',
},
controller: function ($scope) {
// When upload is done
$scope.file = file;
}
};
}
Controller
module.controller('Ctrl', function ($scope) {
$scope.$watch('file', function (val) {
console.log('file', val);
});
});
Template
<div ng-controller='Ctrl'>
<fileuploader on-end='file'></fileuploader>
</div>
This adds some indirection though and is maybe less forward then calling a controller method.