简体   繁体   English

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

[英]angularjs JavaScript inheritance, fail to bind data from view to a controller

what am trying to do is to get data from the form use it in my controller in HTTP post call but it's not working 试图做的是从表单中获取数据,并在HTTP调用中在我的控制器中使用它,但是它不起作用

I know i might have problem with inheritance of scopes but icant solve it. 我知道我可能对范围的继承有疑问,但可以解决。 here is my controller code: 这是我的控制器代码:

.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');
    });

  };

})

Here is the code of my Template: 这是我的模板的代码:

   <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>

Note: i tried the ng-submit its not really the problem 注意:我尝试了ng-submit,这并不是问题所在

Inside the controller. 控制器内部。

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

And this 和这个

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

Also drop the ; 也放下; in ng-click ng-click

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM