简体   繁体   中英

Dynamic ng-init set value in Angularjs

I want to initiate ng-init on page load. I have task.id that fetch with Restangular and my html is:

<textarea rows="5" ng-model="comment.comment_text"></textarea>{{task.id}}
<input type="hidden" ng-model="comment.task_id" ng-init="comment.task_id = $scope.task.id">   
<button type="submit" class="btn btn-success" ng-click="create(comment)">Send</button>

and angular controller:

$scope.create = function(comment) {
     console.log(comment);
};

When click on button show result in console:

{comment_text: "test", task_id: undefined }

But I want to show like this:

{comment_text: "test" ,task_id:53}

Try :

ng-init="comment.task_id = task.id"

or you can set from controller

$scope.comment.task_id  =$scope.task.id

or input value

<input type="hidden" ng-model="comment.task_id" value="{{task.id}}" />  

Try

<textarea rows="5" ng-model="comment.comment_text"></textarea>{{task.id}}
<input type="hidden" ng-model="comment.task_id" ng-init="comment.task_id = task.id">   
<button type="submit" class="btn btn-success" ng-click="create(comment)">Send</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM