JSColor pick up color to trigger AngularJS ng-change

社会主义新天地 提交于 2019-12-12 02:21:33

问题


I use this plugin, JSColor (http://jscolor.com/) and bind the input value with AngularJS ng-model like the following:

<input class="color" ng-model="myColor" ng-change="alert(myColor)">

I expect everytime I pick up a color AngularJS will alert the changed value of myColor, but nothing happens.

What else should I do? thank you! :)


add:

I also tried this:

<input class="color" ng-model="myColor"> 
<textarea style="color:#{{myColor}}>texttext</textarea> 

to bind text color in textarea, also doesn't works.


回答1:


Angular expressions (as in ng-change) are evaluated against the scope, so window's objects are not visible. If you want to call alert on change you can create a function on the scope that does it:

$scope.alert = function(myColor) {
    alert(myColor);
}

Or with injecting the $window service:

.controller('ctrl', function($scope, $window) {
    $scope.alert = function(color) {
        $window.alert(color);
    }
})



回答2:


Your on-change is not fired because of the alert called. ng-change="alert(myColor)"

   $scope.alert = function(myColor) {
   alert(myColor);
}


来源:https://stackoverflow.com/questions/29600230/jscolor-pick-up-color-to-trigger-angularjs-ng-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!