Since I am a beginner using AngularJS the $scope
approach to pass data between different controllers and (in my case) a modal drives me crazy. Due to that reason, I googled around the web and found an interesting blogpost about passing data to a UI-bootstrap modal without using $scope
.
I had a deeper look at this blogpost and the delivered plunk which works pretty nice and started to adopt this to my own needs.
What I want to achieve is to open a modal delivering an text input in which the user is able to change the description of a given product. Since this would provide more than a minimal working example I just broke everything down to a relatively small code snippet available in this plunk.
Passing data from the main controller into the modal seems to work as the default product description is displayed in the modal text input as desired. However, passing the data back from the modal to the main controller displaying the data in index.html
does not seem to work, since the old description is shown there after it was edited in the modal.
To summarize my two questions are:
- What am I doing wrong in oder to achieve a 'two-way-binding' from the main controller into the modal's text input and the whole way back since the same approach works in the mentioned blogpost (well, as the approach shown in the blogpost works there must be something wrong with my code, but I cannot find the mistakes)
- How can I implement a proper
Accept
button in order to accept the changed description only if this button is clicked and discard any changes in any other case (clicking onCancel
button or closing the modal by clicking next to it)?
In your main controller, create two resolver functions:
getDescription
andsetDescription
.In your modal controller, use them.
Your modal HTML
Your main controller
Your modal controller