last day (15 days later) » 

7:48 PM
0
A: Switch view in view in AngularJS upon click

FlorianTopfIf you want to add set the selectedItem to the newly added element just do this in the addPost function: var newPatient = {name: $scope.title, age: $scope.age}; $scope.patients.push(newPatient); $scope.selectedItem = newPatient; Then the new element will be displayed inside the viewbox element.

 
Thanks for the answer! But my problem is not so much how to add a new patient. Say you select a patient from my select box, and it shows in my viewbox. (Which works). What I am trying to achieve, if possible, is when selected patient is in the viewbox, there is going to be a button. When you click on this button (in the view), the view changes to another, in which you can edit said patient. Hope it makes it more clear :)
 
Of course there is. The easiest way to do that is to make an action for the button which fills the ng-model (age, title) from the input with the data from the selected patient. The only thing you need to take care of is, that you distinguish between an edit and an add submission. On edit you just update the patient in the array. I hope that makes it clear. If not I can elaborate an example.
 
If you have the time to make an example, I'd much appreciate it! :)
 
You propably need this because you want to replace the updated patient in the array right? If yes, I can give you an example how to do it without needing the $index.
 
Well, to be honest, it is not updating or creating new patients. It's to show the view, with the details of the patient I select in the list. Does that make sense or should I make it more clear? :)
 
7:48 PM
I'd appreciate it. I thought you wanted to fill the input with the data you have selected so that you can update a patient...
 
Okay! so I have this array of objects (with patient data) in my app.js. In my view, I've made a select list with options. When I select one of the patients, the viewbox (which is on the same page) gets updated with details of the patient I selected. Now, my next step, is to make a button within this viewbox with the patient details, which will link to a page or partial with the complete info of this patient. (In this, I will make the data editable, but is not the focus right now). Sorry to have been unclear, hope this helps! :)
I could do <button><a href="#/details/{{selectedItem.id}}">Rediger</a></button>. This will take me to my details page, but it is not dynamic. I mean, id's are manually entered. Or am I just making this overly complicated?
 
I would do the same thing, but why do you think this is not "dynamic"?
if the id is really unique you can go for this solution.
 
Well, considering that the .id is from the selfwritten data, I don't know if it will work when I hook it up to a database as mongo? or would it still work?
 
As I said, as long as the ids are unique there should be no problem. can you guarantee that?
 
Well, I think so. That might come to a test. One last questions. If these id's are indeed unique, will it be possible to (with crud) update the data depending on these id's?
question*
 
8:26 PM
yes sure, that is all you need!
 
Ah, but I see the problem is now, that when I push a new patient to the array (which works), it doesn't get an id, which means it doesn't link to it. That's what I meant by dynamically. Trying to scour the internet for answers right now. Must be possible to get the index of an array using ng-model.
 
yes you can I can give you an example... I'm just in the middle of something, hang on a few minutes :)
 
No haste, I really appreciate your help!
 
8:53 PM
I think the easiest solution would be to do the ng-select differently:
ng-options="values.indexOf(patients) as patients.name for patients in patients"
then you have saved the index in the array in the ng-model
so you would be able to work with the array index in the whole flow.
 
Aaah, thank you! I just worked it in, and it works perfectly!
Thank you so much for your help, been great!
 
can I put this into my answer so that you can accept it? I think I deserve some reputation :P
 

last day (15 days later) »