AngularJS Passing function with arguments to event directive
To start off, I know there are loads of similar questions but none that I found which supports execution of arbitrary methods with event listeners.
I have this directive which executes functions passed when the window resizes.
app.directive('onResize', function() {
var directive = {
'link': function(scope, element, attrs) {
var onResizeHandler = scope.$eval(attrs.onResize);
angular.element(window).on('resize', onResizeHandler);
angular.element(window).on('$destory', function() {element.off();});
}
};
return directive;
});
I can trigger the above directive with
<div data-on-resize="stickyHeader">...</div>
Which runs my method, inside my controller.
app.controller('myController', [$scope, function($scope) {
$scope.stickyHeader = function() {
console.log('event triggered')
};
}]);
All the above code works fine, but I need to pass some arguments to stickyHeader
as in data-on-resize="stickyHeader(arg1, arg2)"
When I try that, I get Cannot read property 'call' of undefined at ng (angular.js:3795)
in the console. Not sure what I can do to make my directive support arbitrary methods with arguments.
angularjs
add a comment |
To start off, I know there are loads of similar questions but none that I found which supports execution of arbitrary methods with event listeners.
I have this directive which executes functions passed when the window resizes.
app.directive('onResize', function() {
var directive = {
'link': function(scope, element, attrs) {
var onResizeHandler = scope.$eval(attrs.onResize);
angular.element(window).on('resize', onResizeHandler);
angular.element(window).on('$destory', function() {element.off();});
}
};
return directive;
});
I can trigger the above directive with
<div data-on-resize="stickyHeader">...</div>
Which runs my method, inside my controller.
app.controller('myController', [$scope, function($scope) {
$scope.stickyHeader = function() {
console.log('event triggered')
};
}]);
All the above code works fine, but I need to pass some arguments to stickyHeader
as in data-on-resize="stickyHeader(arg1, arg2)"
When I try that, I get Cannot read property 'call' of undefined at ng (angular.js:3795)
in the console. Not sure what I can do to make my directive support arbitrary methods with arguments.
angularjs
Possible duplicate of Pass argument to directive in angularjs?
– Akber Iqbal
Nov 23 '18 at 4:34
checkout stackoverflow.com/questions/29099814/…
– Akber Iqbal
Nov 23 '18 at 4:34
Neither of the suggested duplicates apply as it is not wise to create event handler directives with isolate scope or interpolation.
– georgeawg
Nov 23 '18 at 5:04
add a comment |
To start off, I know there are loads of similar questions but none that I found which supports execution of arbitrary methods with event listeners.
I have this directive which executes functions passed when the window resizes.
app.directive('onResize', function() {
var directive = {
'link': function(scope, element, attrs) {
var onResizeHandler = scope.$eval(attrs.onResize);
angular.element(window).on('resize', onResizeHandler);
angular.element(window).on('$destory', function() {element.off();});
}
};
return directive;
});
I can trigger the above directive with
<div data-on-resize="stickyHeader">...</div>
Which runs my method, inside my controller.
app.controller('myController', [$scope, function($scope) {
$scope.stickyHeader = function() {
console.log('event triggered')
};
}]);
All the above code works fine, but I need to pass some arguments to stickyHeader
as in data-on-resize="stickyHeader(arg1, arg2)"
When I try that, I get Cannot read property 'call' of undefined at ng (angular.js:3795)
in the console. Not sure what I can do to make my directive support arbitrary methods with arguments.
angularjs
To start off, I know there are loads of similar questions but none that I found which supports execution of arbitrary methods with event listeners.
I have this directive which executes functions passed when the window resizes.
app.directive('onResize', function() {
var directive = {
'link': function(scope, element, attrs) {
var onResizeHandler = scope.$eval(attrs.onResize);
angular.element(window).on('resize', onResizeHandler);
angular.element(window).on('$destory', function() {element.off();});
}
};
return directive;
});
I can trigger the above directive with
<div data-on-resize="stickyHeader">...</div>
Which runs my method, inside my controller.
app.controller('myController', [$scope, function($scope) {
$scope.stickyHeader = function() {
console.log('event triggered')
};
}]);
All the above code works fine, but I need to pass some arguments to stickyHeader
as in data-on-resize="stickyHeader(arg1, arg2)"
When I try that, I get Cannot read property 'call' of undefined at ng (angular.js:3795)
in the console. Not sure what I can do to make my directive support arbitrary methods with arguments.
angularjs
angularjs
edited Nov 23 '18 at 3:10
Airerr
asked Nov 23 '18 at 3:00
AirerrAirerr
676
676
Possible duplicate of Pass argument to directive in angularjs?
– Akber Iqbal
Nov 23 '18 at 4:34
checkout stackoverflow.com/questions/29099814/…
– Akber Iqbal
Nov 23 '18 at 4:34
Neither of the suggested duplicates apply as it is not wise to create event handler directives with isolate scope or interpolation.
– georgeawg
Nov 23 '18 at 5:04
add a comment |
Possible duplicate of Pass argument to directive in angularjs?
– Akber Iqbal
Nov 23 '18 at 4:34
checkout stackoverflow.com/questions/29099814/…
– Akber Iqbal
Nov 23 '18 at 4:34
Neither of the suggested duplicates apply as it is not wise to create event handler directives with isolate scope or interpolation.
– georgeawg
Nov 23 '18 at 5:04
Possible duplicate of Pass argument to directive in angularjs?
– Akber Iqbal
Nov 23 '18 at 4:34
Possible duplicate of Pass argument to directive in angularjs?
– Akber Iqbal
Nov 23 '18 at 4:34
checkout stackoverflow.com/questions/29099814/…
– Akber Iqbal
Nov 23 '18 at 4:34
checkout stackoverflow.com/questions/29099814/…
– Akber Iqbal
Nov 23 '18 at 4:34
Neither of the suggested duplicates apply as it is not wise to create event handler directives with isolate scope or interpolation.
– georgeawg
Nov 23 '18 at 5:04
Neither of the suggested duplicates apply as it is not wise to create event handler directives with isolate scope or interpolation.
– georgeawg
Nov 23 '18 at 5:04
add a comment |
1 Answer
1
active
oldest
votes
The directive needs to evaluate the AngularJS expression defined by the on-resize
attribute every time the events occurs:
app.directive('onResize', function($window) {
var directive = {
link: function(scope, elem, attrs) {
angular.element($window).on('resize', onResizeHandler);
scope.$on('$destroy', function() {
angular.element($window).off('resize', onResizeHandler);
});
function onResizeHandler(event) {
scope.$eval(attrs.onResize, {$event: event});
scope.$apply();
}
}
};
return directive;
});
Also since the resize
event comes from outside the AngularJS framework, the event needs to be brought into the AngularJS execution context with $apply()
.
Further, to avoid memory leaks, the event handler needs to be unbound when the scope is destroyed.
Usage:
<div data-on-resize="stickyHeader($event)">...</div>
For more information, see AngularJS Developer Guide - $event.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440167%2fangularjs-passing-function-with-arguments-to-event-directive%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The directive needs to evaluate the AngularJS expression defined by the on-resize
attribute every time the events occurs:
app.directive('onResize', function($window) {
var directive = {
link: function(scope, elem, attrs) {
angular.element($window).on('resize', onResizeHandler);
scope.$on('$destroy', function() {
angular.element($window).off('resize', onResizeHandler);
});
function onResizeHandler(event) {
scope.$eval(attrs.onResize, {$event: event});
scope.$apply();
}
}
};
return directive;
});
Also since the resize
event comes from outside the AngularJS framework, the event needs to be brought into the AngularJS execution context with $apply()
.
Further, to avoid memory leaks, the event handler needs to be unbound when the scope is destroyed.
Usage:
<div data-on-resize="stickyHeader($event)">...</div>
For more information, see AngularJS Developer Guide - $event.
add a comment |
The directive needs to evaluate the AngularJS expression defined by the on-resize
attribute every time the events occurs:
app.directive('onResize', function($window) {
var directive = {
link: function(scope, elem, attrs) {
angular.element($window).on('resize', onResizeHandler);
scope.$on('$destroy', function() {
angular.element($window).off('resize', onResizeHandler);
});
function onResizeHandler(event) {
scope.$eval(attrs.onResize, {$event: event});
scope.$apply();
}
}
};
return directive;
});
Also since the resize
event comes from outside the AngularJS framework, the event needs to be brought into the AngularJS execution context with $apply()
.
Further, to avoid memory leaks, the event handler needs to be unbound when the scope is destroyed.
Usage:
<div data-on-resize="stickyHeader($event)">...</div>
For more information, see AngularJS Developer Guide - $event.
add a comment |
The directive needs to evaluate the AngularJS expression defined by the on-resize
attribute every time the events occurs:
app.directive('onResize', function($window) {
var directive = {
link: function(scope, elem, attrs) {
angular.element($window).on('resize', onResizeHandler);
scope.$on('$destroy', function() {
angular.element($window).off('resize', onResizeHandler);
});
function onResizeHandler(event) {
scope.$eval(attrs.onResize, {$event: event});
scope.$apply();
}
}
};
return directive;
});
Also since the resize
event comes from outside the AngularJS framework, the event needs to be brought into the AngularJS execution context with $apply()
.
Further, to avoid memory leaks, the event handler needs to be unbound when the scope is destroyed.
Usage:
<div data-on-resize="stickyHeader($event)">...</div>
For more information, see AngularJS Developer Guide - $event.
The directive needs to evaluate the AngularJS expression defined by the on-resize
attribute every time the events occurs:
app.directive('onResize', function($window) {
var directive = {
link: function(scope, elem, attrs) {
angular.element($window).on('resize', onResizeHandler);
scope.$on('$destroy', function() {
angular.element($window).off('resize', onResizeHandler);
});
function onResizeHandler(event) {
scope.$eval(attrs.onResize, {$event: event});
scope.$apply();
}
}
};
return directive;
});
Also since the resize
event comes from outside the AngularJS framework, the event needs to be brought into the AngularJS execution context with $apply()
.
Further, to avoid memory leaks, the event handler needs to be unbound when the scope is destroyed.
Usage:
<div data-on-resize="stickyHeader($event)">...</div>
For more information, see AngularJS Developer Guide - $event.
edited Nov 23 '18 at 9:14
answered Nov 23 '18 at 4:49
georgeawggeorgeawg
34.2k115370
34.2k115370
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440167%2fangularjs-passing-function-with-arguments-to-event-directive%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Pass argument to directive in angularjs?
– Akber Iqbal
Nov 23 '18 at 4:34
checkout stackoverflow.com/questions/29099814/…
– Akber Iqbal
Nov 23 '18 at 4:34
Neither of the suggested duplicates apply as it is not wise to create event handler directives with isolate scope or interpolation.
– georgeawg
Nov 23 '18 at 5:04