Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

$scope.testme = "inputname",

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

the below result:

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

Thank you!

like image 729
user1342336 Avatar asked Sep 21 '14 20:09

user1342336


People also ask

What is the name attribute in HTML?

The name attribute specifies a name for an HTML element. This name attribute can be used to reference the element in a JavaScript. For a <form> element, the name attribute is used as a reference when the data is submitted. For an <iframe> element, the name attribute can be used to target a form submission.

What directive is used to specify a controller in an HTML element?

The ng-controller directive adds a controller to your application. In the controller you can write code, and make functions and variables, which will be parts of an object, available inside the current HTML element.

What is name and value in HTML?

The HTML <input> name Attribute is used to specify a name for an <input> element. It is used to reference the form-data after submitting the form or to reference the element in a JavaScript. Syntax: <input name="name"> Attribute Values: It contains a single value name which describes the name of the <input> element.

Which directive is used to render contents of a variable as HTML in safe manner?

The ng-bind-html directive is a secure way of binding content to an HTML element.


1 Answers

Please see more 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>
like image 172
sylwester Avatar answered Nov 08 '22 08:11

sylwester