how to call a function from template literal on ng-click

Issue

I am calling a function close_popup from template literal. I want to pass a variable id. Below is the code

function register_popup(id, name) {

            var htmlelement = `<div class="popup-box chat-popup" id="${id}">
                                    <div class="popup-head background-purple-gradient">
                                        <div class="popup-head-left">${name}-${id}</div>
                                        <div class="popup-head-right">
                                            <span style="cursor:pointer" ng-click="close_popup(${id})">&#10005;</span>
                                        </div>
                                        <div style="clear: both"></div>
                                    </div>
                                    <div class="popup-messages">
                                        <div ng-repeat="message in chatMessages" ng-class="{'message-right': message.align == 'right', 'message-left': message.align == 'left'}">
                                            <div class="message-text">{{message.msg}}</div>
                                                <span class="message-time">{{message.time}}</span>
                                            </div>
                                        </div>
                                        <md-divider></md-divider>
                                        <form ng-submit="sendMsg()" emoji-submit="sendMsg()" emoji-form emoji-message="new_message">
                                            <div class="popup-input-wrapper md-padding" layout="row" layout-align="start center">
                                                <div class="message-form input-wrapper md-padding" layout="row" layout-align="start center">
                                                    <div flex layout="row" layout-align="center center">
                                                        <input style="font-size:15px" type="text" class="input-control" id="popup-message-input" ng-model="new_message.message" placeholder="Type a message..." autofocus flex/>
                                                    </div>
                                                    <md-button id="emojibtn" class="md-icon-button" aria-label="close">
                                                        <md-icon md-svg-src="cloud/client/assets/svg/smile.svg"></md-icon>
                                                    </md-button>
                                                    <md-button class="md-icon-button" id="send-message" aria-label="close">
                                                        <md-icon md-svg-src="cloud/client/assets/svg/send.svg"></md-icon>
                                                    </md-button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>`



            var compiledElement = $compile(htmlelement)(scope);

            var pageElement = angular.element(document.getElementById("chat-room"));
            pageElement.append(compiledElement);

            scope.popUps.push(id);  
            calculate_popups();
        }

The function gets invoked but id which is passed as a parameter is undefined. How can I pass parameter correctly?

Solution

I think you are missing quotes to the id since it is supposed to be a string literal.

ng-click="close_popup('${id}')"

UPDATE:

How come ng-click="close_popup('${id}') works but not ng-click="close_popup(${id})"

This is a little tricky to explain because we have JavaScript’s template literal inside an AngularJS template.

When you call register_popup you’ll pass it an id which is a string value. Its value (say popupId) will be injected into the template string. So by the time angular gets your template, all the ${id} will be replaced by popupId. What Angular sees is: ng-click="close_popup(popupId)". It will treat popupId as a variable defined on $scope and try looking it up (that’s why you got undefined).

What you actually wanted to pass to Angular is ng-click="close_popup('popupId')" thus you need the extra quotes.

You can do a console.log(htmlelement)before you compile it. That will help you understand what the string becomes after it’s evaluated.

Answered By – jian

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.