问题
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