Невозможно ng-повторять директивы в angularjs

0

Я новичок в angularjs. Я хочу динамически добавлять директивы в div, основанные на названиях от контроллера. Ниже я пытаюсь...

Ниже приведены директивы и завод, которые я использовал

     QtTemplate.directive("shortanswer",function()
  {
    return{
        restrict:"A",

        templateUrl:'assets/directives/shortAnswer.html'
    }   
  })

    Template.factory("questionList",function()
  {
    var questionList={};
    questionList.testid="1";
    questionList.name="Maths";
    questionList.Questions = 
    [
        {"title":"truefalse"},
        {"title":"shortanswer"} 
    ]
    return questionList;
  })


 Template.controller("questionControl",['$scope','questionList',function($scope,questionList)
  {
    $scope.name = questionList.name;
    $scope.id = questionList.testid;
    $scope.Questions = questionList.Questions;
  }])
Template.directive('dynamicDirective',function($compile)
  {
    return {
      restrict: 'A',scope:{},
      replace: false, 
      terminal: true, 
      priority: 1000, 
      link:function(scope,element,attrs)
      {
        element.attr(scope.$eval(attrs.dynamicDirective),"");
        element.removeAttr("dynamic-directive"); //remove the attribute to avoid indefinite loop
        element.removeAttr("data-dynamic-directive");
        $compile(element)(scope);
      }
    };
  });
 QtTemplate.directive("shortanswer",function()
  {
    return{
        restrict:"A",

        templateUrl:'assets/directives/shortAnswer.html'
    }   
  })

Ниже приведен пример использования ng-repeat

<div ng-repeat="Questions in Questions" >
    <div dynamicDirective="{{Questions.title}}"  ></div>
</div>

Это не работает

Теги:

3 ответа

3

использование-кебаб-случай-в-HTML

<div ng-repeat="Question in Questions" >
    <div dynamic-directive="{{Question.title}}"  ></div>
</div>
  • 0
    все еще не работает
  • 0
    @bharath вам нужно другое имя для итератора.
Показать ещё 4 комментария
2

Вам нужно написать dynamic-directive вместо dynamicDirective в вашем HTML. Camelcase в Javascript нужно написать как snakecase kebab-case в HTML.

  • 0
    Я только что узнал, что случай со змеей - это «dynamic_directive» (подчеркивание как разделитель). Как упомянул @maurycy, это называется кебаб-кейс. См .: stackoverflow.com/questions/11273282/…
0

Вот ты где:

<div ng-repeat="Question in Questions" >
    <div dynamicDirective="{{Question.title}}"  ></div>
</div>

Надеюсь это поможет.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню