Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
should probably go through some tutorials as this is angular 101 –  charlietfl Sep 21 '14 at 21:08

1 Answer 1

up vote 1 down vote accepted

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>

share|improve this answer
    
Thank you. But I need a name attribute in input element, this is the question I asked. –  user1342336 Sep 21 '14 at 22:17
    
@user1342336 do you want set name attribute ? –  sylwester Sep 21 '14 at 22:19
    
@user1342336 please see updated answear –  sylwester Sep 21 '14 at 22:35
    
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. –  user1342336 Sep 22 '14 at 0:12
    
@user1342336 please see update –  sylwester Sep 22 '14 at 8:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.