Jquery + Angular form does not pass an input value

4

I'm doing a post with Angular. It works fine when I manually type the values in the form, however it gets a value via jQuery $("#id_veiculo").val(valores02.id); and this value appears correctly, but at the time of sending the value of this input ends up disappearing.

Follow the code: link

<form class="form-inline" ng-controller="FormController" ng-submit="submitForm()" role="form" method="post">
    <input type="text" class="form-control" id="id_veiculo" ng-model="id_veiculo" value="">  <!-- Esta input só funciona quando digito manualmente o valor, ela recebe por jquery mas na hora de enviar se torna vazia ou nula -->
    <div class="form-group">
        <label class="sr-only">Itens Opcionais:</label>

        <?php
        echo ' <select ng-model="id_opcional">';

        foreach ($opcionais_encontrados as $o):
            echo '<option value="' . $o->id . '">' . $o->opcional . '</option>';
        endforeach;
        echo '</select>';
        ?>
    </div>

    <button type="submit" class="btn btn-default btn-primary">Submit Record</button>
    <pre style="display:none;">{{ message}}</pre>
</form>

o app.js

'use strict';

(function(){
var HC = {};
var App = angular.module('myApp', ['ui.bootstrap']);
var $scope;

HC.FormController = function($scope, $http) {
    $scope.id_veiculo = undefined;
    $scope.id_opcional = undefined;
    $scope.message = undefined;

    $scope.submitForm = function() {        
        $http({
            method: 'POST',
            url: 'http://cipauto/painel/opcionais/add',
            headers: {
                "Content-Type" :  "application/json"
            },
            data: JSON.stringify({id_veiculo: $scope.id_veiculo, id_opcional: $scope.id_opcional})
        }).success(function(data){                              
            var scope = angular.element(document.getElementById("table")).scope();
            scope.rows.push({id_veiculo: $scope.id_veiculo, id_opcional: $scope.id_opcional});
            $scope.rows = scope;
            //alertify.notify(data.message, data.status, 5, function() { console.log(data.message); });
        });
    }
};


HC.TableViewController = function($scope, $http) {
    $http({
        method: 'POST',
        url: 'http://cipauto/painel/opcionais/listAll',
        headers: {"Content-Type":"application/json"},
    }).success(function(data){
        $scope.rows = data;
    });
};

App.controller('TableViewController', HC.TableViewController);
App.controller('FormController', HC.FormController);

return HC;

})();

So I realized the problem should be in $scope.id_veiculo = undefined; . How do I add the value of the input #id_seminovo that is assigned by jQuery above.

    
asked by anonymous 19.09.2015 / 22:02

1 answer

2

Use the proper angle to set the value of the field_id field.

//recupera o scopo do angular
var scope = angular.element('[ng-controller=FormController]').scope();
//define o valor
scope.test2 = 'id_veiculo';
//faz o apply no scope para poder atualizar o valor definido acima na pagina, ou seja, fazer o bind
scope.$apply();

Well Explained Example

Remembering that this form is not used so much, but it breaks a branch for what you want. Ideally, you should better understand how your application works to try to handle this directly in the controller, for example.

    
23.09.2015 / 04:58