【问题标题】:Why custom directive doesn't work为什么自定义指令不起作用
【发布时间】:2015-07-01 14:43:13
【问题描述】:

我正在尝试实现 AngularJS 的自定义指令。

假设代码如下所示。

<!DOCTYPE html>
<html lang='en' ng-app='testApp'>
    <head>
        <meta charset='utf-8'/>
        <meta http-equiv='X-UA-Compatible' content='IE=edge'/>
        <meta name='viewport' content='width=device-width, initial-scale=1'/>
        <script src="angular.js"></script>
        <script src="searchinput.js"></script>
    </head>
    <body>
        <searchInput></searchInput>
    </body>
</html>

searchInput.js

var testApp = angular.module('testApp', []);
testApp.directive("searchInput", function() {
    return {
          replace:true,
          restrict: 'AE',
          template: '<h3>Hello World!!</h3>'
      }
});

很遗憾,我没有看到预期的结果。有谁知道我错过了什么?谢谢。

【问题讨论】:

  • 按照以下答案的指示进行操作。

标签: angularjs


【解决方案1】:

改变

<searchInput></searchInput>

<search-input></search-input>

查看AngularJS documentation了解更多详情

【讨论】:

    【解决方案2】:

    就我而言,即使使用连字符也无济于事。整个标签必须是小写的(在 angularjs/1.3.16 上)。

    我的自定义指令 textReplace 最初不起作用。然后,我检查了text-replace,但也没有用。最后,与textreplace 核对,成功了。

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 2018-06-01
      • 2020-08-04
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      • 2016-12-29
      相关资源
      最近更新 更多