How to put value of $scope to ng-init in input?


#1

Hi, i want to put value of $scope in controller into input value, this is some code.

<input type="hidden" ng-model="comment.user_email" ng-init="comment.user_email='{{post_ID}}'" value="{{post_ID}}"></input>

But it not works. When i use console, it shown :

Object {user_email: "{{post_ID}}"}

Not the value of post_ID.
Anybody have a solution?
Thank you


#2

You’re wrapping quotes around it which is telling it to set it to the string value '{{post_ID}}'.

If you want it to set to the value of post_ID you should do this:

ng-init="comment.user_email = post_ID"

You don’t need to use curly braces when inside an expression. See the ngInit docs.


#3

@brandyshea thank you for reply, but it doesnt works.
The console is still Object {user_email: “post_ID”}
Not the value of post_ID


#4

Can you put together a codepen of this? I tested this in a codepen using the following:

<input ng-init="comment.email = post_ID">
{{ post_ID }}<br>
{{ comment }}

JS:

$scope.comment = {};      
$scope.post_ID = "test";

And it worked as desired.