美文网首页angular
angular自定义指令以及常见错误

angular自定义指令以及常见错误

作者: iqing2012 | 来源:发表于2017-09-25 12:47 被阅读5次

自定义指定

  • angular.module('dir',[ ]).directive( 'mydir' ,function( ){ return {template:'<div>hello</div>'}} )
  • 自定义指定对原有html的属性扩展。
  • 指定的应用:
    • <div mydir> </div>写成自定义属性 ----A----属性
      -<mydir></mydir>写成元素。注意如果需要改变模板路径里面的内容,就需要使用ng-transclude,然后在模板指令的配置项里写
      return {templateUrl :../template.html,transclued:true} -----E----元素
    • 可以在配置项里写templateUrl(scope:{footer:@footer}) (注意@footer中的元素标签里面的footer是属性),

Error: $compile:iscp

Invalid Isolate Scope Definition

Description
When declaring isolate scope the scope definition object must be in specific format which starts with mode character (@&=<), after which comes an optional ?, and it ends with an optional local name.

myModule.directive('directiveName', function factory() {
  return {
    ...
    scope: {
      'localName': '@', // OK
      'localName2': '&attr', // OK
      'localName3': '<?attr', // OK
      'localName4': ' = attr', // OK
      'localName5': ' =*attr', // OK      
      'localName6': 'attr',    // ERROR: missing mode @&=<
      'localName7': 'attr=',   // ERROR: must be prefixed with @&=<
      'localName8': '=attr?',  // ERROR: ? must come directly after the mode
      'localName9': '<*'  // ERROR: * is only valid with =
    }
    ...
  }
});
  • 指令中多个单词采用驼峰命名法('myDir')。在html中可以使用my-dir来写指令,因为html不区分大小写。
    -- templateUrl(scope:{footer:@footer},replace:true)中的replace:true,可以替换掉自定义指定形成的标签。

  • 指定的类的形式:<div class='mydir'></div>,但是因为默认是A||E,所以要加一个匹配模式配置项:
    templateUrl(scope:{footer:@footer}, restrict:C)就可以添加成功。 ------C类------

  • 总共有四种匹配模式:A,C,E,M(注释,比较少用),如果不写匹配模式,默认为A || E.

相关文章

网友评论

    本文标题:angular自定义指令以及常见错误

    本文链接:https://www.haomeiwen.com/subject/qjnrextx.html