0

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}
4
  • 1
    It should be ng-init="comment.task_id = task.id" whithout $scope Commented Mar 17, 2016 at 8:05
  • I test but doesn't work Commented Mar 17, 2016 at 8:23
  • Can your share your full code to give you suggestion Commented Mar 17, 2016 at 8:26
  • I solved problem with add ng-if="task.id!==undefined". how to resolve problem without ng-if Commented Mar 17, 2016 at 10:57

2 Answers 2

1

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}}" />  
Sign up to request clarification or add additional context in comments.

5 Comments

is there $scope.task.id exist?
I solved problem with add ng-if="task.id!==undefined". how to resolve problem without ng-if
Three ways i suggested not work? :) Re-check "task" object of $scope is already pushed to the view. Can you show full code in your controller?
Thanks, but I want implement with ng-init. 2nd solution is true but I want to implement with your 1st solution.
Nice! :) post a part of your code (enough to run) to codepen.io then we can find the best solution.
0

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>

1 Comment

I solved problem with add ng-if="task.id!==undefined". how to resolve problem without ng-if

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.