[英]angular: how to update value when range input modified via code
I'm pretty new to angularJS,我对 angularJS 很陌生,
I have a range input, which might get it's value updated via code.我有一个范围输入,它可能会通过代码更新它的值。 I have created a ng-model
for this input, and have ng-bind
ed it to a <p>
tag.我为这个输入创建了一个ng-model
,并将它ng-bind
到一个<p>
标签。
It works fine as long as the range input is modified by the user, but when it is updated by the code, the updated value is not getting reflected.只要用户修改了范围输入,它就可以正常工作,但是当它被代码更新时,更新的值没有得到反映。
I have a created a fiddle: https://jsfiddle.net/cj1emb1r/16/我创建了一个小提琴: https : //jsfiddle.net/cj1emb1r/16/
How can I make it work?我怎样才能让它工作?
<div ng-app=''>
<input type='range' min='20' max='200' ng-model="amount" id='slider'>
<p ng-bind="amount"> </p>
<button onclick='foo();'> Run!</button>
</div>
The function where the value is modified:修改值的函数:
<script>
var flag = true;
function foo() {
var sliderObj = document.getElementById('slider');
var value = parseInt(sliderObj.value);
if(value >= 200 || value <= 0) {
flag = !flag;
}
if(flag) {
sliderObj.value = value + 5;
}
else {
sliderObj.value = value - 5;
}
console.log(value);
setTimeout("foo()", 200);
}
</script>`
For starters , I suggest you to read some articles in official developer guide documentation.对于初学者,我建议您阅读官方开发人员指南文档中的一些文章。 And walk through tutorial , which is very useful for understanding Angular basics.并通过教程,这对于理解 Angular 基础知识非常有用。
Back to your code , there are couple of fundamental things that you didn't get about Angular.js workflow.回到你的代码,关于 Angular.js 工作流,你没有了解一些基本的东西。 In order to control your model you should to use controllers
.为了控制您的模型,您应该使用controllers
。 The amount
variable that you're using as a model in slider ( via ng-model
) is kept in $scope
or in your case in $rootScope
, I suppose (because there are no any controllers
or directives
/ components
in your app).您在滑块中用作模型的amount
变量(通过ng-model
)保存在$scope
或者在您的情况下保存在$rootScope
,我想(因为您的应用程序中没有任何controllers
或directives
/ components
)。 So, if you want to change amount
value manually, you have to actually change $rootScope.amount
value.因此,如果您想手动更改amount
值,则必须实际更改$rootScope.amount
值。 But without controller
and dependency injection , that attached to it, you simply can't reach $rootScope
variable.但是如果没有controller
和依赖注入,附加到它,你根本无法到达$rootScope
变量。
So, first of all you better define your app module
and then attach controller to it, so you can manage it's $scope
.所以,首先你最好定义你的应用程序module
,然后将控制器附加到它,这样你就可以管理它的$scope
。
Here is my example , how to do that (with comments).这是我的例子,如何做到这一点(带评论)。
But if to speak, it is recommended by style guide to keep variables not in $scope
but in controller
itself.但是如果要说的话, 样式指南建议将变量保存在$scope
而不是在controller
本身中。 Here is how to do that.这是如何做到这一点。
Try this:尝试这个:
In your script
, add -在您的script
,添加 -
var p = document.getElementsByTagName('p')[0];
after the inner if-else
loop, add p.innerHTML=sliderObj.value;
在内部if-else
循环之后,添加p.innerHTML=sliderObj.value;
Angularjs bindings can only be accessed inside angularjs controllers
. Angularjs 绑定只能在 angularjs controllers
访问。 Plain JavaScript doesn't recognize angularjs bindings.普通 JavaScript 无法识别 angularjs 绑定。 Hence, it doesn't change the value of p
accordingly.因此,它不会相应地改变p
的值。
if i understand , you can call to foo如果我明白,你可以打电话给foo
foo();
and by default set value和默认设置值
value = 155;
before you call foo function在调用 foo 函数之前
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.