【问题标题】:angularjs JavaScript inheritance, fail to bind data from view to a controllerangularjs JavaScript继承,无法将数据从视图绑定到控制器
【发布时间】:2015-03-03 18:39:19
【问题描述】:

我想做的是从表单中获取数据,在我的控制器中使用它HTTP post call 但它不起作用

我知道我可能对范围继承有问题,但我无法解决它。 这是我的控制器代码:

.controller('SignUpCtrl', function($scope, $http, $state) {  
 $scope.submit = function() {

   var url = 'http://localhost:3000/register';
   var user = {
        email: $scope.email,
        password: $scope.password,
      };
     console.log($scope.user);
   $http.post(url, user)
     .success(function(res){
        console.log('You are now Registered');
        //$state.go('app.items');
    })
     .error(function(err){
        console.log('Could not register');
         //  $state.go('error');
    });

  };

})

这是我的模板代码:

   <form name="register">
        <div class="list">
            <label class="item item-input no-border">
                <input name="fullname" type="text" ng-model="fullname" placeholder="Full Name" required="">
            </label>
            <label class="item item-input">
                <input name="email" type="email" ng-model="user.email" placeholder="Email" required="">
            </label>
            <p class="blue-font" ng-show="register.email.$dirty && register.email.$invalid">Please Write valid Email.</p>
            <label class="item item-input">
                <input name="password" type="password" ng-model="user.password" placeholder="Password" required="">
            </label>
            <button ng-click="submit();" ng-disabled="register.$invalid" type="submit" class="button signup-btn sharb-border white-font blue-bg-alt border-blue-alt ">
                Sign Up
            </button>
        </div>
    </form>

注意:我尝试了 ng-submit 它并不是真正的问题

【问题讨论】:

    标签: javascript angularjs data-binding


    【解决方案1】:

    在控制器内部。

        .controller('SignUpCtrl', function($scope, $http, $state) {
          $scope.user = {
            email: '',
            password: ''
          };
          $scope.submit = function() {
    

    还有这个

        var user = {
          email: $scope.user.email,
          password: $scope.user.password
        };
    

    同时将; 放入ng-click

        <button ng-click="submit()" ...>
    

    【讨论】:

    • 当我这样做时,我得到了 TypeError: Cannot read property 'email' of undefined
    • 表示$scope中没有user对象。我会编辑答案
    • 是的,问题是我在提交函数中有它。
    【解决方案2】:

    尝试使用 rootScope (docs.angularjs.org/$rootScope")。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 2016-08-11
      • 2013-04-09
      相关资源
      最近更新 更多