javascript - 为什么这个 Angular Directive(指令)只被调用一次?

标签 javascript angularjs angularjs-directive

我定义了以下简单指令

angular.module('my-app').directive('formPanel', function () {

    return {
      restrict: 'EA',
      scope: {
        forms: "=",
        title: "@",
        formSelect: "&"
      },
      templateUrl: 'formPanel.html'
    };
  }
);

formPanel.html 模板的内容是:

<h1>foo</h1>

如果我像这样调用该指令 3 次:

<form-panel title="title" forms="entitledApplications" 
    form-select="goToFormDetails(selectedForm)"/>

<form-panel title="title" forms="maybeEntitledApplications" 
    form-select="goToFormDetails(selectedForm)"/>

<form-panel title="title" forms="applications" 
    form-select="goToFormDetails(selectedForm)"/>

呈现以下输出

<h1>foo</h1>

但我期待以下内容

<h1>foo</h1>
<h1>foo</h1>
<h1>foo</h1>

如果传递给 forms 范围属性的列表为空/null,似乎会出现问题,但我不明白为什么。

最佳答案

尝试为您的指令 HTML 元素使用结束标记:

<form-panel title="title" forms="entitledApplications" 
   form-select="goToFormDetails(selectedForm)"></form-panel>

<form-panel title="title" forms="maybeEntitledApplications" 
   form-select="goToFormDetails(selectedForm)"></form-panel>

<form-panel title="title" forms="applications" 
   form-select="goToFormDetails(selectedForm)"></form-panel>

关于javascript - 为什么这个 Angular Directive(指令)只被调用一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33609084/

相关文章:

javascript - 请解释此代码段

javascript - 错误: [$injector:unpr] Unknown provider: RestangularProvider <- Restangular <- ctrlAG

javascript - 基于翻译字符串的加载指令

javascript - 如何在我的 angularjs 指令中切换 contenteditable 等属性的值?

javascript - yarn : add dependency from subdirectory in git repository

javascript - 没有 jQuery 的 AJAX 不会将 POST 数据发送到 PHP 文件

javascript - 如何使用ajax向服务器提交表单?

javascript - Angular JS $on "$locationChangeStart"在 event.preventDefault() 时触发了两次

javascript - Typescript + Angular 公开服务方法

带过滤器的 AngularJS 表