Form field filled in but given as $ pristine

2

I am making a request $ http, the return is a JSON, attribution to the model ($ scope.elaborator), however the field is still set to $ pristine.

		Data.post('getElaborador', $rootScope.codigo, 'Listagens').then(
			function(success){
				$scope.elaborador = success;								
			}, function(error){
				console.log('Erro ao consulta informações do elaborador');
			});
    .state("perfil", {
        url: "/perfil",
        templateUrl: "views/profile/perfil.html",
        data: {pageTitle: 'Perfil de Usuário'},
        controller: "UserProfileController",
        resolve: {
            deps: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load({
                    name: 'MetronicApp',  
                    insertBefore: '#ng_load_plugins_before',
                    files: [
                    '../assets/global/plugins/angularjs/plugins/ui-select/select.min.css',
                    '../assets/global/plugins/angularjs/plugins/ui-select/select.min.js',

                    '../assets/pages/css/profile.css',
                    '../assets/js/angular-viacep.min.js',

                    'js/controllers/UserProfileController.js'
                    ]                    
                });
            }]
        }
    })
    // User Profile Dashboard
    .state("perfil.cadastro", {
        url: "/cadastro",
        templateUrl: "views/profile/elaborador.html",//onde esta o formulario
        data: {pageTitle: 'Perfil de Usuário'}
    })

<!-- perfil.html -->
<div class="row">
    <div class="col-md-12">
        <!-- BEGIN PROFILE SIDEBAR -->
        <div class="profile-sidebar">
            <!-- PORTLET MAIN -->
            <div class="portlet light profile-sidebar-portlet">
                <!-- SIDEBAR USERPIC -->
                <div class="profile-userpic">
                    <img src="{{settings.assetsPath}}/pages/media/profile/profile_user.jpg" class="img-responsive" alt=""> </div>
                <!-- END SIDEBAR USERPIC -->
                <!-- SIDEBAR USER TITLE -->
                <div class="profile-usertitle">
                    <div class="profile-usertitle-name"> {{dados[0]}} </div>
                    <div class="profile-usertitle-job"> {{(dados[1]==1?'Elaborador':'Membro da Comissão')}} </div>
                </div>
                <!-- END SIDEBAR USER TITLE -->
                <!-- SIDEBAR BUTTONS -->
                <div class="profile-userbuttons">
                    <button type="button" class="btn btn-circle green-haze btn-sm">Follow</button>
                    <button type="button" class="btn btn-circle btn-danger btn-sm">Message</button>
                </div>
                <!-- END SIDEBAR BUTTONS ng-class="{active: $state.includes('profile.dashboard')}"-->
                <!-- SIDEBAR MENU -->
                <div class="profile-usermenu">
                    <ul class="nav">
                        <li ng-class="{active: $state.includes('perfil.cadastro')}">
                            <a ui-sref="perfil.cadastro">
                                <i class="fa fa-user" aria-hidden="true"></i> Cadastro </a>
                        </li>
                        <li ng-class="{active: $state.includes('perfil.formacao')}">
                            <a ui-sref="perfil.formacao">
                                <i class="fa fa-graduation-cap" aria-hidden="true"></i> Formação </a>
                        </li>
                    </ul>
                </div>
                <!-- END MENU -->
            </div>
            <!-- END PORTLET MAIN -->
            <!-- PORTLET MAIN -->
            <div class="portlet light">
              <!-- CAMPOS -->
            </div>
            <!-- END PORTLET MAIN -->
        </div>
        <!-- END BEGIN PROFILE SIDEBAR -->
        <!-- BEGIN PROFILE CONTENT -->
        <div ui-view class="profile-content fade-in-up"> </div>
        <!-- END PROFILE CONTENT -->
    </div>
</div>

// developer.html

<form name="elaboradorFrm" id="elaboradorFrm" data-toggle="validator" role="form" style="margin-top:20px;margin-right:5px;margin-left:5px;" autocomplete="off">
	<div id="dados-pessoais">
		<fieldset>
			<legend style="border-bottom: 1px dashed #f7dfd5;">Dados Pessoais</legend>					
			<div class="row">
				<div class="col-md-8">
					<div class="form-group {{elaboradorFrm.cadNome.$pristine ? '' : elaboradorFrm.cadNome.$invalid ? 'has-error' : 'has-success'}} has-feedback">
						<label for="cadNome">Nome <span>*</span></label>
						<input class="form-control" type="text" id="cadNome" name="cadNome" ng-model="elaborador.cadNome" ng-minlength="3" ng-required="true">
						<span class="glyphicon glyphicon-{{elaboradorFrm.cadNome.$invalid && elaboradorFrm.cadNome.$dirty ? 'remove' : elaboradorFrm.cadNome.$pristine ? '' : 'ok'}} form-control-feedback" aria-hidden="true"></span>
					</div>
				</div>
			</div>
		</fieldset>
	</div>
</form>
    
asked by anonymous 15.12.2016 / 14:38

1 answer

1

When you programmatically assign a value, use $ setDirty () to remove the pristine state from form.

Example:

Data.post('getElaborador', $rootScope.codigo, 'Listagens').then(
        function(success){
            $scope.elaborador = success;                                
            $scope.nomeDoFormulario.$setDirty();
        }, function(error){
            console.log('Erro ao consulta informações do elaborador');
        });
    
15.12.2016 / 16:24