Javascript 将值绑定到 Angular JS 中的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12421979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:56:13  来源:igfitidea点击:

Binding value to input in Angular JS

javascriptangularjs

提问by mTuran

I have input like this

我有这样的输入

<input type="text" name="widget.title" ng-model="widget.title" value="{{widget.title}}"/>

I want to change input value dynamically so i use that but it doesn't change the value:

我想动态更改输入值,所以我使用它,但它不会更改值:

$scope.widget.title = 'a';

回答by Dan Caragea

You don't need to set the value at all. ng-model takes care of it all:

您根本不需要设置该值。ng-model 负责这一切:

  • set the input value from the model
  • update the model value when you change the input
  • update the input value when you change the model from js
  • 设置模型的输入值
  • 更改输入时更新模型值
  • 当您从 js 更改模型时更新输入值

Here's the fiddle for this: http://jsfiddle.net/terebentina/9mFpp/

这是为此的小提琴:http: //jsfiddle.net/terebentina/9mFpp/

回答by Frank6

If you don't wan't to use ng-model there is ng-valueyou can try.

如果您不想使用 ng-model,则ng-value可以尝试。

Here's the fiddle for this: http://jsfiddle.net/Rg9sG/1/

这是这个的小提琴:http: //jsfiddle.net/Rg9sG/1/

回答by Er. Sushil Singh

Use ng-valuefor set value of input box after clicking on a button:

使用ng-value的点击一个后输入框的设定值button

"input type="email"  class="form-control" id="email2" ng-value="myForm.email2" placeholder="Email"

and

Set Value as:

将值设置为:

 $scope.myForm.email2 = $scope.names[0].success;

回答by Danny Mora

Some times there are problems with funtion/features that do not interact with the DOM

有时会出现不与 DOM 交互的功能/特性的问题

try to change the value sharply and then assign the $scope

尝试大幅更改值,然后分配 $scope

document.getElementById ("textWidget") value = "<NewVal>";
$scope.widget.title = "<NewVal>";

回答by Hablu

{{widget.title}} Try this it will work

{{widget.title}} 试试这个它会工作