8

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!

0

1 Answer 1

5

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>

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. But I need a name attribute in input element, this is the question I asked.
@user1342336 do you want set name attribute ?
I tested it, the {{myElement.name}} seemed not equal to "myInput". Can I remove ng-model? I have an old html code, when name="something", a value would be assigned to 'true' or 'false' for a checkbox. So I don't need put ng-model there. If I put ng-model="true" or "false", the checkbox will take it's value, so name attribute is useless here, now I still want to use the old value assigned to name attribute. Sorry, it is a little bit messy. Thank you for your wonderful answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.