Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs form, input value changed programmatic(by code) not submitted to controller

my problem is that after change input value by code or any plugin new value not submitted to controller and old value of property is accessible. but if change input value by typing new value is available! only by typing! template :

 <input class="form-control" id="ng-taskLineBackColor" 
       type="text" ng-model="data.preference.lineBackColor"/>

 <input type="submit" ng-click="update()"  class="btn btn-primary" value="save"/>

controller :

.controller('taskCtrl', ['$scope', function ($scope) {

        $scope.getRef = function () {
            return //any code
        };
        $scope.save = function () {
             var newValue = $scope.data.preference.lineBackColor;
             //!!!-->newValue Contain old Value

        };
    }])
like image 830
sajjad Avatar asked Feb 14 '23 14:02

sajjad


2 Answers

Any code which changes the value of ng-taskLineBackColor needs to trigger a special event called "input". This will notify AngularJS

$(function() {
    $('#ng-taskLineBackColor').val('new value').trigger('input');
});
like image 193
Chet Avatar answered Feb 16 '23 04:02

Chet


To do this only with jQlite and without jQuery try:

angular.element(document.querySelector('#ng-taskLineBackColor')).triggerHand‌​ler('input')

And here's the API you have available on an angular.element-wrapped HTML element:

  • https://docs.angularjs.org/api/ng/function/angular.element
like image 35
Jan Molak Avatar answered Feb 16 '23 04:02

Jan Molak