Javascript 如何给 html 元素属性赋值 angularjs 变量值,比如输入元素的 name 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25963591/
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-22 22:09:28  来源:igfitidea点击:

How to assign angularjs variable value to html element attribute, such as input elememnt's name attribute

javascripthtmlangularjs

提问by user1342336

I am totally new to angular, I have an angularjs $scope variable:

我对 angular 完全陌生,我有一个 angularjs $scope 变量:

$scope.testme = "inputname",

and I want to assign this variable value to a name attribute of html element. I want

我想将此变量值分配给 html 元素的 name 属性。我想要

the below result:

以下结果:

<input name="inputname" ...>. But how to get it from angularjs scope variable?

<input name="inputname" ...>. 但是如何从 angularjs 范围变量中获取它呢?

Thank you!

谢谢!

采纳答案by sylwester

Please see more https://docs.angularjs.org/guide/databinding

请参阅更多https://docs.angularjs.org/guide/databinding

enter image description here

在此处输入图片说明

var app= angular.module("app",[]).controller('fCtrl', function($scope){

$scope.elementName = "testName";
$scope.myElement = {
  value:"some value",
  name:"myInput"
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="fCtrl">
  
  <input type="text" ng-model="myElement.value" name="{{myElement.name}}">
  <hr/>
  name only
  <br/>
  
  <input type="text" name="{{elementName}}">
  </div>
  
</div>