AngularJS Ng-model没有来自表单的数据

时间:2016-02-29 09:06:22

标签: javascript angularjs

<form class="" ng-submit="submit()" ng-controller="MailingListController">
        <input class="form-element large" placeholder="Email address" ng-model="emailaddress">
        <input class="form-submit button large bkg-charcoal bkg-hover-pink color-white color-hover-white" type="submit" id="submit" value="Submit">

</form>

                   

<script>
    angular.module('ComingSoon', [])
        .controller('MailingListController', ['$scope', function($scope) {
        console.log("Working");
        $scope.submit = function() {
            console.log($scope.emailaddress);
        };
        }]);
</script>

我试图提交此表单并且日志显示$ scope.emailaddress中没有任何内容。我已经关注了angular网站上的文档,但它仍然无效。我在错误地做错了什么?

2 个答案:

答案 0 :(得分:0)

控制器绑定到视图可能出错了。你可以看看  这里有以下plnkr

html: -

<!DOCTYPE html>
<html ng-app="ComingSoon">

<head>
  <link rel="stylesheet" href="style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
  <script src="script.js"></script>
</head>



<body ng-controller="MailingListController">

  <form class="" ng-submit="submit()">
    <input class="form-element large" placeholder="Email address" ng-model="emailaddress">
    <input class="form-submit button large bkg-charcoal bkg-hover-pink color-white color-hover-white" type="submit" id="submit" value="Submit">

  </form>


</body>


</html>

控制器: -

 angular.module('ComingSoon', [])
        .controller('MailingListController', ['$scope', function($scope) {
        console.log("Working");
        $scope.submit = function() {
            console.log($scope.emailaddress);
        };
        }]);

在做!!时检查你的控制台。

https://plnkr.co/edit/B7aJhGKhXin1tsldljpz?p=preview

答案 1 :(得分:0)

嗨,这对我有用

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
    <title></title>
	<meta charset="utf-8" />
    <script src="Scripts/angular.js"></script>
    <script>
    angular.module('ComingSoon', [])
        .controller('MailingListController', ['$scope', function ($scope) {
            console.log("Working");
            $scope.submit = function () {
                console.log($scope.emailaddress);
            };
        }]);
    </script>
</head>
<body ng-app="ComingSoon">
    <form class="" ng-submit="submit()" ng-controller="MailingListController">
        <input class="form-element large" placeholder="Email address" ng-model="emailaddress">
        <input class="form-submit button large bkg-charcoal bkg-hover-pink color-white color-hover-white" type="submit" id="submit" value="Submit">
    </form>
</body>
</html>
&#13;
&#13;
&#13;