4

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

        };
    }])

3 Answers 3

6

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');
});
Sign up to request clarification or add additional context in comments.

1 Comment

Does this behavior really exists in Angular? A.K.A. is it documented?
2

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:

Comments

0

I create working JSfiddle for your case.

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 function "save", declared in controller on "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 + "'");
    };
}

5 Comments

no!my condition is that value modified by code.for time typing value there is no problem.
thanks for your trying but pay attention to this subject: modify programaticall(by code) html element value you modify property of object!.in this sample change input value by any code or plug in then get new value!thanks
@sajjad, what you mean under "html element value"? Value, which shows in input?
yes. ok! for this sample html element value is value of input text type.
@sajjad, in my JSfiddle example, I change value of input text at controller. $scope.data.preference.lineBackColor = "Value from code"; After that, you will see new text-value in input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.