簡體   English   中英

Angular.js中的動態ng-init設置值

[英]Dynamic ng-init set value in Angularjs

我想在頁面加載時啟動ng-init task.id獲取的Restangular ,我的html是:

<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>

和角度控制器:

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

當單擊按鈕時,在控制台中顯示結果:

{comment_text: "test", task_id: undefined }

但我想這樣顯示:

{comment_text: "test" ,task_id:53}

嘗試:

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

或者您可以從控制器設置

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

或輸入值

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

嘗試

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM