Dynamically added directives not responding

心不动则不痛 提交于 2019-12-12 18:22:59

问题


I am building an app which loads some data from server and according to this data includes (appends) directives into the dom.
Here is the main page html:

<div ng-repeat="type in bet.bet_types">
    <div ng-include src="getBetTypeById(type.id)"></div>
</div>

Here is the getBetTypeById(id) function from the scope:

    $scope.getBetTypeById = function(id)
    {
        switch(id)
        {
            case 1:
                return '/views/partials/1.html';
                break;  
...

Here is the 1.html:

<test-test bettype={{type}}></test-test>

here is the tets-test directive:

var app = angular.module('soccerWinner', []);

app.directive('test-test', function()
{
    return {
        restrict: 'E',
        replace: true,
        scope:
        {
            bettype: '='
        },
        templateUrl: '/views/partials/directives/bettaype_soccer_winner.html',
        controller: function()
        {
            alert('dfd');
        }
    };
});

And here is the bettaype_soccer_winner.html:

<h2>test</h2>

There is no errors in the console but there is also no alert shown, as seen is the directive controller.

What did I do wrong?


回答1:


Change directive name to testTest. It should be camelcase in definition.

Angular normalizes an element's tag and attribute name to determine which elements match which directives. We typically refer to directives by their case-sensitive camelCase normalized name (e.g. 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 (e.g. ng-model).

https://docs.angularjs.org/guide/directive



来源:https://stackoverflow.com/questions/24360468/dynamically-added-directives-not-responding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!