【问题标题】:how to call a function from template literal on ng-click如何在 ng-click 上从模板文字调用函数
【发布时间】:2018-09-07 02:18:55
【问题描述】:

我正在从模板字面量调用函数 close_popup。我想传递一个变量 id。下面是代码

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();
        }

函数被调用,但作为参数传递的 id 未定义。如何正确传递参数?

【问题讨论】:

    标签: angularjs ecmascript-6 angularjs-ng-click template-literals


    【解决方案1】:

    我认为您缺少 id 的引号,因为它应该是字符串文字。

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

    更新

    为什么ng-click="close_popup('${id}') 有效但ng-click="close_popup(${id})" 无效

    这有点难以解释,因为我们在 AngularJS 模板中有 JavaScript 的模板字面量。

    当您调用register_popup 时,您将向其传递一个字符串值的 id。它的值(比如popupId)将被注入到模板字符串中。因此,当 Angular 获取您的模板时,所有 ${id} 将被 popupId 替换。 Angular 看到的是:ng-click="close_popup(popupId)"。它将popupId 视为在$scope 上定义的变量并尝试查找它(这就是您未定义的原因)。

    您真正想要传递给 Angular 的是 ng-click="close_popup('popupId')",因此您需要额外的引号。

    您可以在编译之前执行console.log(htmlelement)。这将帮助您了解字符串在评估后会变成什么。

    【讨论】:

    • 谢谢它。为什么 id="${id}" 工作但不是 ng-click="close_popup(${id})"
    • @ot954 你需要一个反引号 ` 来表示 ES6 中的字符串模板文字。例如“${id}
    • @Niladri 这不是我的问题。对不起,如果我不清楚。正如您在代码中看到的那样,我使用了反引号,但我不清楚为什么 id="${ id }" 工作时 ng-click="close_popup(${id})" 在字符串模板中不起作用
    • @ot954 那是因为参数如果是字符串参数,需要用引号引起来。
    • @ot954 我添加了更多解释。您可以将htmlelement 登录到控制台以更好地理解它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2014-01-15
    • 2017-08-21
    • 1970-01-01
    相关资源
    最近更新 更多