javascript - 未调用模板代码中的函数

标签 javascript angularjs

在我调用的代码中,例如addComment,为什么从HTML do directiv 模板粘贴后她不起作用?我该怎么办。

第二个问题是两者之间有什么区别

modal-window-card card="card"

modal-window-card="card"

因为我发现我可能只分配了变量 card.name 并且在第一个示例中我拥有整个对象,但为什么?

HTML:

 <div modal-window-card card="card"></div>  

指令:

App.directive('modalWindowCard', function () {
    return {
        scope: {
            card: "=", 
        },
        template: `
        <div id="{{$parent.$parent.$index}}/{{$parent.$index}}" class="modalDialog">
            <div>
                <a href="#close" title="Close" class="close">X</a>
                <div>
                    <div class="windows-title">
                        <span class="header">{{ card.name }} </span>
                    </div>
                    <div class="window-header">
                        <p class="quiet-header">W liscie {{ item.list }}</p>
                    </div>

                    <div class="windows-description">
                        <span class="loud-header">Dodaj komentarz:</span>
                        <form ng-submit="addComment($parent.$index, $index, commentCard)">
                            <input id="text2" ng-model="commentCard" cols="30" rows="5"> 
                          </form>
                            <button ng-click="addComment($parent.$index, $index, commentCard)" style="margin-top:5px;">Zapisz</button>
                       </div>

                        <div class="window-activity">
                            <span class="loud-header">Aktywnosc</span>
                            <span class="quiet-header" style="float: right; margin-right: 310px;">Pokaz Szczególy</span>
                            <div class="window-comments">
                                <div style="border-bottom: 1px solid #E2E4E6; margin-top: 5px;" ng-repeat="comment in card.comments">
                                    <span class="name-header">{{ comment.author }}</span>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>`
   }
})

最佳答案

您应该使用@而不是=

scope: {
        card: "@", 
    }

在 div 元素中

<div modal-window-card card="card value is here"></div>

您正在使用 modal-window-card 调用指令“modalWindowCard”,并将名为“card”的属性传递给该指令的隔离范围属性。

在这种情况下

<div modal-window-card="card value is here"></div>

Angularjs 尝试查找指令,但无法解析此“=”。 modal-window-card="card value is here"中的名称“card”不是您可以将其发送到指令的属性。

我修改了您的代码,请检查here

关于javascript - 未调用模板代码中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46513647/

相关文章:

javascript - 打印出 api 回调时得到未定义/无结果

javascript - 在其范围之外使用 Promise 的 .then 方法中的函数

javascript - Java EE 应用程序的 UI 技术

javascript - AEM 6.1 复选框启用/禁用标签属性

javascript - SVG绘制两个矩形之间的连接线

mysql - 使用 Sequelize 在 nodejs 中将数组转换为批量创建

javascript - Angular ng-repeat 导致闪烁

javascript - angularjs在没有jquery的情况下将css属性添加到元素

angularjs - 使用 AngularJS 进行分页?

angularjs - Angular js 从默认值初始化 ng-model