1

I would like to understand how to do the following, they asked me to try and create a function in the controller instead of writing the code in the view.

The code is:

 <div class="row">
                <div class="col-xs-12 max300" 
                     uib-dropdown is-open="vm.descriptionDropdownOpen">
                    <textarea name="description" class="form-control" 
                              ng-model="vm.presence.description" 
                              ng-click="vm.toggleDescriptionDropdown()"
                              autofocus>
                    </textarea>
                    <ul id="descriptionDropdown" uib-dropdown-menu>
                        <li ng-repeat="descr in vm.loadedDescriptions" 
                            ng-click="vm.presence.description = descr.text;
                                      vm.descriptionDropdownOpen = false;">
                            {{descr.text}}
                        </li>
                    </ul>
                </div>
            </div>

so basically this creates a text box, and when you click on it, a dropdown menu will appear, and if you click on a string of the dropdown menu, that string will be put in the text box.

What I need to do is to create a function that will be put in the controller, so we can just call that function in the view and keep the code nicer.

This function just needs to do the last part of the code I posted above, take the string I click on from the dropdown menu and put it in the text box!

It's really simple but as I'm learning I'm not that sure on how I should write it

setDescription(text: string) {
        // code should go here.
}

Sorry for this stupid question, just wanna be sure to understand correctly what I am doing! thank you

2
  • Is your view code not already doing the same thing? ng-click="vm.presence.description = descr.text; Commented Dec 6, 2017 at 15:40
  • yes! but as i'm learning, they asked me how could I implement this with a function in the controller to keep the code clean in the view!! Commented Dec 6, 2017 at 22:00

2 Answers 2

1

html

 ng-click="vm.submitString(descr.text)"

controller

vm.submitString = function(text){
    return vm.presence.description = text;
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much for the answer, I'm trying to figure out how can I put your code in the input they gave me, they said to implement that with a function in the controller, giving me the beginning of the function being: setDescription(text: string) { // code should go here. } so if I get it right, with your code, should it be something like: html: ng-click="setDescription(descr.text)" controller: setDescription(text: string) { return vm.presence.description = text; } is this correct? thank you again!
i think setDescription(text: string) {} is latest angular versions so yea it would work as well
0

resolved like this:

setDescription(text: string) { 
       return this.presence.description = text; 
}

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.