angularjs JavaScript继承,无法将数据从视图绑定到控制器

时间:2015-03-03 18:39:20

标签: javascript angularjs data-binding

我要做的是从表单中获取数据,在HTTP帖子调用中使用它在我的控制器中,但它不起作用

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

.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,这不是问题

2 个答案:

答案 0 :(得分:0)

控制器内部。

    .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()" ...>

答案 1 :(得分:-1)

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