2

I have an input with type color defined in my controller scope:

HTML:

<div ng-controller="MyCtrl">
    <input type="color" value="#f0f0f0" />
    <input type="color" value={{getColor()}} />
</div>

JS:

function MyCtrl($scope) {
    $scope.getColor = function () {
        return "#f0f0f0";
    };
}

The problem is the color don't get updated when its set by Angular, although when inspecting I see this:

enter image description here

See: FIDDLE.

How to update html5 input color dynamically?

1
  • I don't know whether the JSFiddle has been updated, but the color was changing for me. Just had a slight delay. Commented Nov 12, 2014 at 9:18

2 Answers 2

4

Try to bind it to your controller with ng-model instead of value.

function MyCtrl($scope) {  
    $scope.mycolor = "#f0f0f0";

    $scope.$watch('mycolor', function(newVal) {
        console.log('newVal ' + newVal);
    });
}

Here is updated and working fiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

Getting rid of value did the trick, I eventually defined the default value using ng-init, see: jsfiddle.net/01qL3tck/13
0

You don`t even need a $watch, just a $timeout.

function MyCtrl($scope, $timeout) { 
    $timeout(() => {
        $scope.mycolor = "#f0f0f0";
    }) 
}

working fiddle http://jsfiddle.net/3ukL3suf/

Comments

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.