简体   繁体   English

AngularJS指令添加其他不起作用的指令

[英]AngularJS directive to add other directives not functional

I am trying to construct a directive that adds form groups to a particular div. 我正在尝试构造一个将表单组添加到特定div的指令。 I'm attempting doing this by binding a directive to a button in my html. 我正在尝试通过将指令绑定到html中的按钮来执行此操作。 My application is VERY simple as this is all I'm trying to do at the moment. 我的应用程序非常简单,因为这是我目前正在尝试做的所有事情。 Similar to this fiddle Anyway, my app initiates fine and the home controller is included. 类似于此小提琴,无论如何,我的应用程序启动良好,并且包含了家庭控制器。 Also I get 200 status codes on the inclusion of my directive, and my code throws no errors. 另外,在包含指令的情况下,我还会得到200个状态代码,并且我的代码不会引发任何错误。 Here is my html: 这是我的html:

  <form id="addFields">
<div class="row">
  <div class="col-xs-4">
  </div>
  <div class="col-xs-4 text-center">
    <div addInputFieldsButton></div>
    <input id="fieldName" placeholder="Enter field name:"></input>
    <button id="addFieldBtn" addInputFields><span class="glyphicon glyphicon-plus"></span></button>
  </div>
  <div class="col-xs-4">
  </div>
</div>
</form>
<div id="reviewFields">
</div>

Notice I am attempting both to add the button that is supposed to add input fields, and just bind the directive that adds input fields to an existing button as an attribute. 请注意,我正在尝试添加应该添加输入字段的按钮,并且仅绑定将输入字段添加为属性的现有按钮的指令。 Neither work. 都不起作用。

addInputFields directive: addInputFields指令:

(function () {
  angular.module('reviewModule')
    .directive('addInputFields', addInputFields);

  addInputFields.$inject = ['$log'];
  function addInputFields ($log) {
    return function (scope, element, attr) {
      $log.debug('binding click event to add review button now.');
      element.bind('click', function ($compile) {
        $log.debug('button bound.');
        angular.element(document.getElementById('reviewFields')).append($compile("<button>YOU MADE A BUTTON, COOL BRO</button>")(scope));
      });
    }
  }
})()

and my directive for attempting to add a button that has the above binding: 以及我的尝试添加具有以上绑定的按钮的指令:

(function () {
  angular.module('reviewModule')
    .directive('addInputFieldsButton', addInputFieldsButton);

  addInputFieldsButton.$inject = ['$log'];
  function addInputFieldsButton ($log) {
    return {
      restrict : 'E',
      template : '<input id="fieldName" placeholder="Enter field name:"></input>\
        <button id="addFieldBtn" addInputFields><span class="glyphicon glyphicon-plus"></span></button>'
    };
  };
})()

I copied the fiddle almost exactly, and really have no idea why nothing is happening while attempting to use either of these directives. 我几乎完全复制了小提琴,并且真的不知道为什么在尝试使用这两个指令时什么也没发生。 Forgive me if my error is obvious, I am still pretty new to AngularJS. 如果我的错误很明显,请原谅我,我对AngularJS还是很陌生。

I believe your second directive is not defined correctly on UI, it should be - separated with smaller case add-input-fields instead of addInputFields . 我相信你的第二个指令没有在UI正确定义,它应该-分离较小的情况下, add-input-fields ,而不是addInputFields

Code

(function () {
  angular.module('reviewModule')
    .directive('addInputFieldsButton', addInputFieldsButton);

  addInputFieldsButton.$inject = ['$log'];
  function addInputFieldsButton ($log) {
    return {
      restrict : 'E',
      template : '<input id="fieldName" placeholder="Enter field name:"></input>\
        <button id="addFieldBtn" add-input-fields><span class="glyphicon glyphicon-plus"></span></button>'
                                //^^^^^^^^^^^^^^^here is change
    };
  };
})()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM