首页 > 解决方案 > 显示数据时出错 - 范围 - AngularJS

问题描述

我在使用 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}},数据不显示。我能指望一点帮助吗?

标签: angularjscodeigniter-3

解决方案


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


推荐阅读