简体   繁体   English

用Angular创建一个简单的指令

[英]Creating a simple directive with Angular

I am having a very simple problem and I can't figure out why it's not working. 我有一个非常简单的问题,我不知道为什么它不起作用。

reviewCtrl.html reviewCtrl.html

angular.module('liveAPP.review',['LiveAPP.factory'])
.controller('reviewCtrl', ['$scope','$http','dataFactory','$location',reviewCtrl])

.directive("datePicker", function() {
    return {
        restrict: "E",
        template: '<div>I want this to show up in my review view</div>'
  }      
})

review.html review.html的

<datePicker></datePicker>

My expectation of this code is to see "I want this to show up in my review view" in the view corresponding to this controller. 我希望此代码是在与该控制器相对应的视图中看到“我希望它显示在我的评论视图中”。 The view is injected into the <div ng-view></div> in my index.html. 该视图被注入到我的index.html中的<div ng-view></div>中。 I have a very simple example here, but for some reason its not working how I would expect. 我在这里有一个非常简单的示例,但是由于某种原因,它无法按我期望的那样工作。 Assume my routes are set up correctly. 假设我的路线设置正确。 Anyone have any idea why this might be? 有人知道为什么会这样吗?

Use 采用

  <date-picker> 

in your html it will work. 在您的html中它将起作用。

Because Angular converts camelCasing to snake-casing, so your datePicker directive needs to be renamed to date-picker in html. 由于Angular将camelCasing转换为snake-casing,因此您的datePicker指令需要在html中重命名为date-picker。


Why to use CamelCasing: 为什么要使用CamelCasing:


Normalization: 正常化:

Angular normalizes an element's tag and attribute name to determine which elements match which directives. Angular对元素的标记和属性名称进行归一化,以确定哪些元素与哪些指令匹配。 We typically refer to directives by their case-sensitive camelCase normalized name (eg ngModel). 我们通常通过区分大小写的camelCase规范化名称(例如ngModel)来引用指令。 However, since HTML is case-insensitive, we refer to directives in the DOM by lower-case forms, typically using dash-delimited attributes on DOM elements (eg ng-model). 但是,由于HTML不区分大小写,因此我们以小写形式引用DOM中的指令,通常在DOM元素(例如ng-model)上使用破折号分隔的属性。

The normalization process is as follows: 规范化过程如下:

Strip x- and data- from the front of the element/attributes. 从元素/属性的前面去除x-和data-。 Convert the :, -, or _-delimited name to camelCase. 将:,-或_分隔的名称转换为camelCase。


See this example: http://jsfiddle.net/kevalbhatt18/khz8vxs4/1/ 参见以下示例: http : //jsfiddle.net/kevalbhatt18/khz8vxs4/1/

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

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