3

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...

6
  • 1
    what you mean not working? Commented Aug 13, 2015 at 17:16
  • saveData function not calling when I'm press submit button. Commented Aug 13, 2015 at 17:17
  • try add type="button" to your button tag Commented Aug 13, 2015 at 17:18
  • @Grundy no its not working. Commented Aug 13, 2015 at 17:19
  • try see errors in browser console Commented Aug 13, 2015 at 17:22

2 Answers 2

5

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);
        });
    }

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

5 Comments

yeah it's working on plunker but in my case it's not working. I'm using a nodejs server is the any dependency.
in that case, it seems very likely that you still have another javascript error that is breaking execution of your code before you can try to trigger that saveData function. Do you have any console errors? Also, have you tried linting your js to look for syntax problems?
No there is no console error and no any syntax error.
Other possibilities would be that a file isn't being included in your build process or a dependency is not being included on request of this page. I can't help much more in a non-speculative way without seeing your whole project directory, since the above solution solves the apparent problem :'(
It fails at $console.log('save data function'); because it does not exists - try $log.debug('save data function')... you will need to add the dependency to $log on the line that says function($scope,$http, $log)
0

Angular have different service for print any things in console window of browser. this service name is $log.

$log service has different methods like:

  • $log.log(string_value)
  • $log.warn(string_value)
  • $log.info(string_value)
  • etc.

So, use it like...

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

chat.controller('chatCtrl', function($scope,$http, $log){
    $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(){
        $log.log('save data function');
        $http.post('http://localhost:3000/save/chat/data', {message:$scope.message, username:$scope.username}).
        success(function(result){
            $log.info(result);
        });
    }
});

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.