I've got an irritating problem with data binding using ng-model and button.
The principle of operation of my site:
- My HTML site displays a list of
projects(loaded from external .json file). - Each row has a button named
Editwhich displays amodalcontaining some<input type="text"filled with relevant data about project (likeproject.name,project.dateetc.) - Initial
valueof input is equal to object data (text-input calledNamewill containproject.nameetc.) - Object is modified only if you click
Savebutton and confirm the operation (confirm(sometext)is okay). Closing the modal, not clicking the button or pressingcancelon confirmation box should prevent data from being updated. - Editing input (let's say that
project.nameis "Project2" and I modify it by adding 3 numbers resulting in "Project2137"), closing modal and opening it again should result in "Project2" text inside input (because object wasn't modified, only input)
So far I understand that single text input should look like this
<input type="text" id="editName" class="form-control" ng-model = "project.name">
Using ng-model means that they are binded. That's what I know. However editing input means that object is updated as soon as I enter some data.
I tried to fiddle with ng-model-options but I didn't find any possible solutions.
I tried to do it programmatically as well using
<input type="text" id="editName" class="form-control" value = {{project.name}}>
....
<button type="button" class="btn pull-right btn-primary btn-md" ng-click="edit(project)" data-dismiss="modal" >Save</button>
And function:
$rootScope.edit = function(project)
{
if(confirm("Are you sure to save changes?"))
{
project.name = angular.element(document.getElementById('editName')).val();
// ...and so on with other properties
This solution is kinda close to what I wanted to achieve (object is updated only on confirm), but I faced another problem: input loads data from object only once at the beginning instead of each time the modal is opened which is against rule #5
Is there any way to fix this using either ng-model bind or custom function? Or maybe there is some other, easier way?
--EDIT--
Here I don't have any problem with saving the data using a button, everything works well and clicking Save is reflected in a projects list. (well until I hit a F5 key).
The problem is that input text is not properly binded to project and that's what I want to fix.
Sample data (pseudocode)
project1.name = "Proj1" project2.name = "Proj2"
I click an Edit button on row #1
- Text input displays "Proj1". Everything is fine.
- I change input by adding some random characters like "Proj1pezxde1"
- Text input is now "Proj1pezxde1"
- I do not click
Savebutton. - I close the modal.
- Project summary still displays "Proj1". Okay.
- I click an edit button on first row
10. Text input is "Proj1pezxde1" even though I didn't modify an object.
Text input should read data from object again (each time I open this modal) and thus display "Proj1"
That's the problem I want to fix. Sorry for being a little bit inaccurate.