0

I've some fields, they can be edited. The user can cancel the edition mode with the button 'Cancel':

<button ng-click="cancelEdit();" ng-show="editable">
    <i class="fa fa-remove"></i> Cancel
</button>

The event ng-click call the function cancelEdit().

$scope.cancelEdit = function(){
    $scope.editable=false;
    $scope.jobNameInput = $scope.jobToView.name;
    $scope.selectedPriority = $scope.jobToView.priority;
    $scope.jobCommentsInput = $scope.jobToView.comments;
}

In this function, I just want to set the boolean variable for edition mode to false and reinit the values of my inputs to the default value (before edition mode). After this function is calling, the values are updated for the controller, but not in my view:

<button ng-click="editable=true;" ng-show="!editable">
    <i class="fa fa-edit"></i> Edit
</button>

This button is shown when the variable editable is set to false. So when I click on the cancel button, theoretically, the button Edit must be shown and my inputs should be updated. Why is this not the case ?

4
  • Could you show how are you binding $scope.jobNameInput and the other values in the view? What's the vaue of $scope.jobToView.name? If you could make a snipped or a plnkr, that would be great. So we can assist you better. Commented Jan 19, 2017 at 13:48
  • mmm try so set ng-click="editable=true;" not in the button but with a function .. so ng-click="setEditTrue()" and then $scope.setEditTrue= function(){ $scope.editable=true; } Commented Jan 19, 2017 at 13:48
  • 2
    Try to replace the editable variable to a property of an object, not as a property of the $scope, something like: buttonEditable = {value:true}; Then in your htlml: ng-click="buttonEditable.value=true" Commented Jan 19, 2017 at 14:04
  • When I tried to do the plunker, it worked. I think that it is a problem related to the fact that I display these inputs only if the user clicks on a button before using a ng-class (checked / !checked). An example of input: <input type="text" ng-model="jobCommentsInput" ng-value="jobToView.comments" ng-disabled="!editable" /> Commented Jan 19, 2017 at 15:09

2 Answers 2

1

Primitives are immutable – i.e. its value cannot be changed by invoking functions on it.

Your $scope.editable is a boolean variable which is primitive. That's why the view does not get updated. Its value gets changed only in the closure of your function.

To apply it in your view you should change it to a non primitive value. This could be done if you set it as a property of an object.

E.g.

$scope.isEditable = {
  value:false
}

Then play around with that object. In your case:

Cancel button:

<button ng-click="cancelEdit();" ng-show="isEditable.value">
    <i class="fa fa-remove"></i> Cancel
</button>

Edit button:

<button ng-click="isEditable.value=true;" ng-show="!isEditable.value">
    <i class="fa fa-edit"></i> Edit
</button>

Function:

$scope.cancelEdit = function(){
    $scope.isEditable.value = false;
    $scope.jobNameInput = $scope.jobToView.name;
    $scope.selectedPriority = $scope.jobToView.priority;
    $scope.jobCommentsInput = $scope.jobToView.comments;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks ! I did not know that the primitive types were not updatable. And in terms of input, do you have any idea? Because, in this case, I use the jobToView object.
You could follow the same strategy. e.g. job = {name:"", priority:"", comments:""}. Then use this object properties in the view.
I already use my jobToView object in my view. But when I step through my cancelEdit function, the value of my input is already equal to my default value. As if he does not see the changes I made the time they were edited. While having the display, after having passed my function, it keeps the modifications that I made.
0

Thanks to Korte for the answer on the boolean variable. To complete, with regard to the inputs, I found the answer to my question. Simply declare in the controller:var vm = this;;

And simply, in the model of the input, write: ng-model="vm.jobCommentsInput"

I think so the inputs are considered as my boolean variable and they needs to be objects.

Comments

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.