Search code examples
javascriptangularjsangularjs-ng-click

ng-click not work in div


i've been stuck here for a while, when i'm using "form" tag everything is ok but in "div" tag ...

main.js:

var app = angular.module('demo', []);

app.controller('appctrl', function ($scope) {    
    $scope.start = function (name) {
        console.log(name);}
 }

index.html:

<!DOCTYPE html>
<html ng-app="demo">
    <head>
        <script src="js/angular.min.js"></script>
        <script src="js/main.js"></script>
    </head>
    <body>
        <div id="container" ng-controller="appctrl">
            <input id="input" autocorrect="off" spellcheck="false" autocapitalize="off" autofocus="true" placeholder="Please enter your full name" type="text" ng-model="name">
            <div id="button" ng-click="show()">Start Demo</div>
        </div>
    </body>
</html>

Solution

  • app.controller('appctrl', function ($scope) { 
        $scope.name = null;   
        $scope.show = function (name) {
            console.log(name);
        }
    });
    <div id="container" ng-controller="appctrl">
                <input id="input" autocorrect="off" spellcheck="false" autocapitalize="off" autofocus="true" placeholder="Please enter your full name" type="text" ng-model="name">
                <div id="button" ng-click="show(name)">Start Demo</div>
            </div>
    

    You're calling show() but you defined start()