Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs: ng-click not working

Here its my html code

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>

<body ng-app="chat">
    <div ng-controller="chatCtrl">
        <div ng-repeat="i in chatResult">
            {{i.first_name}}
            {{i.last_name}}
            {{i.id}}
            {{i.age}}
        </div>
        <input type="text" ng-model="message">{{message}}
        <input type="text" ng-model="username">{{username}}
        <button ng-click="saveData()">Submit</button>
    </div>
    <script src="static/jquery.js"></script>
    <script src="static/angular.min.js"></script>
    <script src="static/app.js"></script>
</body>
</html>

In above html code when I'm press submit button ng-click function not respond.

here its my app.js

var chat = angular.module('chat', [])

chat.controller('chatCtrl', function($scope,$http){
    $scope.chatResult = null;
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencsoded";
    $http.get('http://localhost:3000/chat').success(function(result){
        $scope.chatResult = result;
    });

    $scope.message = '';
    $scope.username = '';
    $scope.saveData = function(){
        $console.log('save data function');
        $http.post('http://localhost:3000/save/chat/data', {message:$scope.message, username:$scope.username}).
        success(function(result){
            $console.log(result);
        });
    }
});

When the submit button is clicked, I call an angular click event to trigger the saveData function.

but it's not working in my case any problem in my code...

like image 380
NIKHIL RANE Avatar asked Feb 10 '26 19:02

NIKHIL RANE


1 Answers

I think it is being called, actually, but I'm not sure about the $console.log. I changed it to just console.log and I see the function logging each time I click the button. Check it out:

http://plnkr.co/edit/BNuZUe?p=preview

var chat = angular.module('chat', [])

chat.controller('chatCtrl', function($scope,$http){
    $scope.chatResult = null;
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencsoded";
    $http.get('http://localhost:3000/chat').success(function(result){
        $scope.chatResult = result;
    });

    $scope.message = '';
    $scope.username = '';
    $scope.saveData = function(){
        console.log('save data function');
        $http.post('http://localhost:3000/save/chat/data', {message:$scope.message, username:$scope.username}).
        success(function(result){
            $console.log(result);
        });
    }

});
like image 183
wesww Avatar answered Feb 12 '26 15:02

wesww



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!