如何将angularjs变量值赋给html元素属性,例如输入elememnt的name属性

时间:2014-09-21 20:34:47

标签: javascript html angularjs

我对角度全新,我有一个angularjs $ scope变量:

$scope.testme = "inputname",

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

以下结果:

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

谢谢!

1 个答案:

答案 0 :(得分:4)

请参阅更多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>