ng-change的注册功能不会被解雇

时间:2016-01-01 16:49:56

标签: javascript angularjs

每当更改文本字段的输入值时,我都会尝试实现ng-change

<html>
    <body>
    <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js'></script>

        <div ng-app='transactionStatus'>

            <form ng-controller='formController'>
                <input type='text' ng-change='responder()' ng-model='message.query' />
                Reply: <span ng-bind='message.response'></span>
            </form>

        </div>  

        <script>


            function formController($scope) {
                    $scope.message = {query : 0};

                    $scope.responder = function(){
                        $scope.message.response = $scope.message.query * 100;
                        $scope.message = message; 
                    }
                }

        </script>

    </body>
</html>

因此,每当输入值发生变化时,应该调用函数responder,并且应该为ng-bind='message.response'写入一个返回的文本但是当我输入文本值时它没有写入任何内容。这可能是什么原因?

2 个答案:

答案 0 :(得分:0)

您必须将控制器添加到角度模块。 将脚本更改为

<script>
    var app = angular.module('transactionStatus', []);
    app.controller('formController', function($scope) {
         $scope.message = {query : 0};

         $scope.responder = function(){
             $scope.message.response = $scope.message.query * 100;
             $scope.message = message; 
         }
    });
</script>

请参阅this示例。

答案 1 :(得分:0)

$scope.message = message;搞乱了这里的事情

试试这个:

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

app.controller('MainCtrl', function($scope) {
  $scope.message = {
    query: 0
  };

  $scope.responder = function() {
    $scope.message.response = $scope.message.query * 100;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
</head>

<body ng-controller="MainCtrl">

  <form>
    <input type='text' ng-change='responder()' ng-model='message.query' /> Reply: 
    <span ng-bind='message.response'></span>
  </form>
</body>

</html>

Plunker here