显示数据时出错-范围-AngularJS

时间:2018-11-09 17:16:44

标签: angularjs codeigniter-3

我在使用AngularJS显示数据时遇到问题。

所以我的应用程序基于AngularJS和CodeIgniter 3。

我已经以CodeIgniter的形式创建了一个验证,一切正常。

public function create()
    {
        $this->form_validation->set_error_delimiters('','');
        $this->form_validation->set_rules( 'login' , 'Imię' , 'required|min_length[3]' );
        $this->form_validation->set_rules( 'email' , 'Email' , 'required|valid_email|is_unique[users.email]' );
        $this->form_validation->set_rules( 'password' , 'Hasło' , 'required|matches[passconf]' );
        $this->form_validation->set_rules( 'passconf' , 'Powtórz hasło' , 'required|matches[password]' );

        if ( $this->form_validation->run())
        {
            $user = $this->input->post('user');
            unset($user['passconf']);
            $user['password'] = crypt($user['password'], config_item('encryption_key'));
            $this->Users_model->create($user);
        }

        else
        {
            $errors['login'] = form_error( 'login' );
            $errors['email'] = form_error( 'email' );
            $errors['password'] = form_error( 'password' );
            $errors['passconf'] = form_error( 'passconf' );
            echo '{"records":' . json_encode( $errors ) . '}';
        }


    }

在AngularJS方面,我希望出现错误。

controllersAdmin.controller('userCreate', function( $scope, $http, $timeout ){

  $scope.user = {};
  $scope.user.role = 'user';

   $scope.createUser = function( user ){
    $http({
      method: 'POST', url: 'api/admin/users/create/' , 
      data: {
        user : user,
        login : user.login,
        email : user.email,
        password : user.password,
        passconf : user.passconf
      }}
      ).then(function ( errors ){ 

        if ( errors )
      {
        $scope.errors = errors;
      }
      else
      {
        $scope.success = true;
        $timeout(function(){
          $scope.success = false;
          $scope.user = {};
        } , 3000 );
      }


   },function (error){
      console.log('Blad we wczytywaniu danych');
   });
   }
});

我创建了$scope.errors = errors;

当我用{{errors}}显示它时-显示数据。

{"data":{"records":{"login":"Pole Imię jest wymagane.","email":"Pole Email jest wymagane.","password":"Pole Hasło jest wymagane.","passconf":"Pole Powtórz hasło jest wymagane."}},"status":200,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"api/admin/users/create/","data":{"user":{"role":"user"}},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":"OK","xhrStatus":"complete"}

但是,当我输入{{errors.login}}时,数据不会显示。我可以指望一点帮助吗?

1 个答案:

答案 0 :(得分:2)

有问题。登录不是错误对象的属性,而是子属性。应该是errors.data.records.login。