【问题标题】:Form value display in angular JS [duplicate]角度JS中的表单值显示[重复]
【发布时间】:2014-03-20 17:44:34
【问题描述】:

我使用 Angular js 创建了一个简单的表单。单击提交按钮时,我希望输入的详细信息显示在它下方,还希望详细信息以 JSON 格式保存在控制台中。

查看代码:

<html lang="en" ng-app="person_info">
<head>
  <meta charset="utf-8">
  <title>Person info</title>
  <script src="angular.min.js"></script>
  <script src="controller_class2.js"></script>
</head>

<body ng-controller="info">
  Name: <input type="text" ng-model="person.name"> </br></br>
  First Name : <input type="text" ng-model="person.firstname"></br></br>
  Phone Number : <input type="text" ng-model="person.number"></br></br>
  Email : <input type="email" ng-model="person.email"></br></br>
  Address : <input type="text" ng-model="person.address"></br></br>
  <input type="submit" value="submit" id="submit" name="submit" ng-click="test()">

  <p>{{person.name}}</p>

</body>
</html>

控制器代码:

var person_info = angular.module('person_info', []);
person_info.controller('info', function($scope) {   
$scope.test = function(){
$scope.person = person;
console.log($scope.person);
}
});

好像出了点问题。我没有得到正确的输出

【问题讨论】:

    标签: javascript jquery angularjs


    【解决方案1】:

    好的,this 是什么意思?

    .controller('info', function($scope) {
    $scope.test = function () {
        $scope.newPerson = $scope.person;
        console.log($scope.newPerson);
    }
    

    【讨论】:

      【解决方案2】:

      模型绑定自动绑定到范围属性。

      行:

      $scope.person = person;
      

      在您的方法中使用未定义的对象覆盖正确的人。如果您删除此行,该方法将起作用。

      $scope.test = function(){
        console.log($scope.person);
      }
      });
      

      【讨论】:

        【解决方案3】:

        看看这个,它有效:http://jsfiddle.net/YWfSF/4/
        你已经覆盖了$scope.person = person; //person is undefined

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-04
          • 1970-01-01
          • 2020-07-10
          • 1970-01-01
          • 2017-03-28
          • 2019-05-13
          • 2018-07-20
          • 2021-09-16
          相关资源
          最近更新 更多