Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS adding ng-click in directive

I'm implementing drag'n'drop directive. On drop I add a copy of element to my div and append ng-click attribute to it like this:

copy.append('<button class="close" ng-click="abc()">&times;</button>');

For example, in controller I have

$scope.abc = function () {
    alert('Hello!');
}

And it doesn't work. If I add this button on page manually it works fine.

like image 382
ChruS Avatar asked Nov 22 '12 09:11

ChruS


People also ask

Can we use NG-click in Div?

Syntax of using ng-click directive There, the button is an element that can be replaced by any other HTML element like a link, span, paragraph, div etc.

How does ng-Click work in AngularJS?

The ng-click Directive in AngluarJS is used to apply custom behavior when an element is clicked. It can be used to show/hide some element or it can pop up an alert when the button is clicked. Parameter Value: expression: It specifies when the particular element is clicked then the specific expression will be evaluated.

Can we use NG-click and Onclick together?

For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.

What is the difference between Ng-click and Onclick?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.


1 Answers

copy.append('<button class="close" ng-click="abc()">&times;</button>');
$compile(copy)($scope);
like image 183
ChrisOdney Avatar answered Oct 23 '22 18:10

ChrisOdney