I want to pass a variable to a function by using the directive ng-click, here is my code:
var name = "John Doe";
var $element = $('<button ng-click="open(name)" + "Name:" + '</button>').appendTo('#user-list');
$compile($element)($scope);
$scope.open = function(name) {
alert(name); //Will display 'undefined'
}
The append works fine and when I'm inserting open(123), I can see an Alert of '123'. But a simple variable like var name will be undefined. Can you please tell me what I'm doing wrong?
<button ng-click="open(name)" ng-show="someCondition">{{name}}</button>. When you click the button, it will launch$scope.open( $scope.name ). Creating a button and appending it to the DOM is the jQuery way. If you want to develop with Angular, just FORGET JQUERY and do it the Angular way.