Answer the question
In order to leave comments, you need to log in
Angular - how to change ng-model via controller?
Good afternoon.
There are applications written in Angular, it has two fields input type="range" and input type="number" so that when you click on input type="number" the slider changes ( input type="range" ). Solved this problem like this
<div ng-app ng-controller="Ctrl">
Range: <input type="range" ng-model="i" min="0" max="5" step=".5" /><br />
<br />
Number: <input type="number" ng-model="i" min="0" max="5" /><br />
<br />
i: {{i}}
</div>
function Ctrl($scope) {
$("input[type=number]").change(function(){
$scope.i = $("input[type=number]").val();
})
}
Answer the question
In order to leave comments, you need to log in
Angular is such a bastard that he does not know about any asynchronous calls to anything if he did not make these calls himself.
$("input[type=number]").change(function(){
$scope.i = $("input[type=number]").val();
})
$scope.someShitChange = function(){...}
Tell me, why do you need AngularJS? Or was the project inherited?
jsfiddle.net/GxqfK/1
The controller communicates with the views through $scope, this is essentially the abstraction level. Models and controllers don't know anything about how your view is rendered, they just provide data. But directives are more interesting - they can work with the DOM and encapsulate some kind of logic. For example, in my example, for the range input, a value is taken and converted to a number (it's not clear why this is not there out of the box ... well, that's not the point).
More details can be found in the documentation. I advise you to say goodbye to my example and realize everything that happens there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question