简体   繁体   English

angularjs:ng-click无法正常工作

[英]angularjs: ng-click not working

Here its my html code 这是我的html代码

<!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. 在上面的html代码当我按下提交按钮时,ng-click功能没有响应。

here its my app.js 这是我的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. 单击提交按钮时,我调用角度单击事件来触发saveData函数。

but it's not working in my case any problem in my code... 但在我的情况下,我的代码中没有任何问题......

I think it is being called, actually, but I'm not sure about the $console.log . 实际上,我认为它正在调用,但我不确定$console.log I changed it to just console.log and I see the function logging each time I click the button. 我将其更改为console.log ,每次单击按钮时都会看到函数记录。 Check it out: 看看这个:

http://plnkr.co/edit/BNuZUe?p=preview 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);
        });
    }

});

Angular have different service for print any things in console window of browser. Angular在浏览器的控制台窗口中有任何不同的服务打印。 this service name is $log . 此服务名称为$log

$log service has different methods like: $log服务有不同的方法,如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM