简体   繁体   English

Angularjs:使用路由 - 不编译或不识别自定义指令

[英]Angularjs: Using routing - the custom directive is not compiled or not recognized

I have custom directive which is not updating in the template when routing is called/done. 我有自定义指令,在调用/完成路由时,模板中没有更新。 Below is the code: module.js 下面是代码:module.js

    angular.module('bookApp', [])
       .config(['$routeProvider', function($routeProvider) {
           $routeProvider.when('/bookList', {
             templateUrl: 'bookList.html',
             controller: BookCntrl
            })
            .otherwise({ redirectTo: '/' });
           }
        ])
        .directive('bookDialog', function(){
             return {
                restrict: 'A',
                replace: true,
                transclude: true,
                scope: { title:'@bookTitle' },
                template: '<div>' +
                      '<div class="title">{{title}}</div>' +
                      '<div class="body" ng-transclude></div>' +
                      '</div>'};
        });
    function BookCntrl($scope) {
           //todo
    };

in bookList.html i have 在bookList.html我有

    <div book-dialog bookTitle="Computer Science">
        Description will come here
    </div>

when I run this and goto http://localhost:8080/bookApp/#/bookList . 当我运行它并转到http://localhost:8080/bookApp/#/bookList It does not render/detect the directive and ng-view gets updated but custom directives remains as it is with no change. 它不会呈现/检测指令,并且ng-view会更新,但自定义指令保持不变,没有任何更改。

Please help me on the issue. 请帮我解决这个问题。 May be I am not getting it, what i missing here in the code. 可能是我没有得到它,我在代码中遗漏了什么。 Thanks in advance. 提前致谢。

Change your template file to 将模板文件更改为

<div book-dialog book-title="Computer Science">
    Description will come here
</div>

Notice that instead of bookTitle you would write book-title 请注意,您不会使用bookTitle来编写book-title

Here's an example: http://jsfiddle.net/jaimem/6wmKy/ 这是一个例子: http//jsfiddle.net/jaimem/6wmKy/

Change "bookTitle" in HTML to either of "book-title" or "book:title" or "book_title". 将HTML中的“bookTitle”更改为“book-title”或“book:title”或“book_title”。

HTML is not case Sensitive but Angular is. HTML不是敏感的,但Angular是。

So even though you write "bookTitle" in Angular code writing it in HTML means same as "booktitle". 因此,即使你在Angular代码中编写“bookTitle”,用HTML编写它也意味着与“booktitle”相同。

Angular normalizes an element's tag and attribute name to determine which elements match which directives. Angular规范化元素的标记和属性名称,以确定哪些元素与哪些指令匹配。 The normalization process is as follows: 规范化过程如下:

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

So "book-title" gets converted to "bookTitle" by Angular for its own understanding. 所以“书名”被Angular转换为“bookTitle”,以供自己理解。

Here is the reference from the documentation 以下是文档中的参考资料

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

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