1

ここに jsfiddle があります: https://jsfiddle.net/vikramkute/eq3zpmp9/5/

私は角度が初めてです。実行時にhtmlに追加する必要があるオブジェクトがあります。私はAngular 1.2.25を使用しています

期待される出力は

1 Quest
2 Quest
3 Quest

しかし、最後の値を3回繰り返しています。私の試行錯誤によると、問題は $compile にあると感じています。さまざまなフォーラムで提供されているさまざまなソリューションを試しましたが、何も機能しませんでした。どんな助けでも大歓迎です。ありがとう。

ディレクティブ内(リンク機能内)

            scope.obj =
            [
                {
                    "questionText": "1 Quest"
                },
                {
                    "questionText": "2 Quest"
                },
                {
                    "questionText": "3 Quest"
                }
            ]

            scope.addData = function() {
                for (var i = 0; i < scope.obj.length; i++) {
                    addSlide(scope.obj[i]);
                }
            }

           addSlide = function (obj) {
               scope.singleObj = obj;
               el = $('<div ng-bind="singleObj.questionText"></div>');
               scope.owl.append(el);
               $compile(el)(scope);
           };

出力:

3 Quest
3 Quest
3 Quest

完全なディレクティブは次のとおりです。

angular.module('journeycarousel', [])
    .directive('journeyCarousel', function ($compile) {
        return {
            restrict: 'E',
            templateUrl: '../components/journeyCarousel/journeyCarousel.html',
            transclude: true,
            link: function (scope, element) {

                scope.obj =
                    [
                        {
                            "questionText": "1 Quest"
                        },
                        {
                            "questionText": "2 Quest"
                        },
                        {
                            "questionText": "3 Quest"
                        }
                    ]

                scope.addData = function() {
                    for (var i = 0; i < scope.obj.length; i++) {
                        addSlide(scope.obj[i]);
                    }
                }

                addSlide = function (obj) {
                    scope.singleObj = obj;
                    el = $('<div ng-bind="singleObj.questionText"></div>');
                    scope.owl.append(el);
                    $compile(el)(scope);
                };
            }
        }
    });

上記のコードは簡易版です。これは実際のコードです:

    scope.singleObj = obj;
    el = $('<div class="questionContainer" <div ng-repeat="obj in singleObj"> ng-click="onSlideClick($event,singleObj)"> <div class="item"> <div class="questionSection" ng-bind="singleObj.questionText"></div> <div class="answerSection" ng-bind="singleObj.questionAnswer + singleObj.questionUnitOfMeasure"></div> </div> </div>');
   $('.owl-carousel').owlCarousel('add', el).owlCarousel('update');
   $compile(el)(scope);
4

2 に答える 2

-1

以下の構造を使用してください。おそらく、オブジェクト指向の概念を台無しにしています。

addSlide = function (obj) {
        scope.singleObj = angular.copy(obj);
        el = $('<div ng-bind="singleObj.questionText"></div>');
        scope.owl.append(el);
        $compile(el)(scope);
};
于 2016-09-27T04:58:53.460 に答える