0

I have controller:

function NameController($scope){
        $scope.data = {
            name: 'Alex',
        }
    }

and HTML

    <div ng-controller="NameController">
        <a href="#" onclick="alert( {{data.name}} );"> ClickMe </a>
    </div>

Why it doesn't work and how make it works? Thanx.

3 Answers 3

1

When you call alert in the view, it's actually looking for $scope.alert, which is undefined. You can create an alert function in the controller that will call the javascript alert function:

$scope.alert = function(data){
   alert(data);
};  

You should also use ng-click instead of onclick:

<a href="#" ng-click="alert(data.name)"> ClickMe </a>

ng-click takes an angular expression. If you use onclick, javascript (not angular) attempts to handle the click event, and it doesn't understand what {{data.name}} is.

Demo

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

<a href="#" onclick="alert( '{{data.name}}' );"> ClickMe </a>

Comments

0

You can use ng-click AngularJs Directive

so your HTML should be like:

<div ng-controller="NameController">
    <a href="#" ng-click="alert( {{data.name}} );"> ClickMe </a>
</div>

and in controller you have to define alert function like

 function NameController($scope){
    $scope.alert = function(name) {
        alert(name);
    }
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.