Angular diagram, input value is changed programmatically (by code), not transmitted to the controller

my problem is that after changing the input value by code or any plugin, a new value is available that is not sent to the controller, and the old property value. but if an input change value is entered, enter a new value! just 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('taskCtrl', ['$scope', function ($scope) {

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

        };
    }])
+4
source share
3 answers

Any code that changes the value of ng-taskLineBackColor should raise a special event called "input". This will notify AngularJS

$(function() {
    $('#ng-taskLineBackColor').val('new value').trigger('input');
});
+4
source

To do this with jQlite only and without jQuery, try:

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

And here is the API you have on the angular.element-objected HTML element:

+1

JSfiddle .

JSfiddle

 <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"/>

I renamed the "save" function declared in the controller to "update".

UPDATE:

function MyCtrl($scope) {
    $scope.update = function () {
        var value = $scope.data.preference.lineBackColor;
        alert("Typing value = '" + value + "'");
        $scope.data.preference.lineBackColor = "Value from code";
        var newValue = $scope.data.preference.lineBackColor;
        alert("Typing value = '" + newValue + "'");
    };
}
0
source

Source: https://habr.com/ru/post/1530542/


All Articles