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