簡體   English   中英

將數組對象從控制器傳遞到AngularJS中的自定義指令

[英]Pass array object from controller to custom directive in AngularJS

我試圖將對象的數組從角度控制器傳遞到自定義指令元素,並使用ng-repeat迭代該對象,但出現以下錯誤:[ngRepeat:dupes]

home.js:

home.controller("homeController", function ($scope) {

    $scope.points =[

        {
            "url": '../assets/images/concert.jpg',
            "id":1
        },
        {
            "url": '../assets/images/dance.jpg',
            "id":2
        },
        {
            "url": '../assets/images/music.jpg',
            "id":3
        },
        {
            "url": '../assets/images/jazz.jpg',
            "id":4
        },
        {
            "url": '../assets/images/violin.jpg',
            "id":5
        },
        {
            "url": '../assets/images/music.jpg',
            "id":6
        }
    ];

});

Shareddirectives.js:

var sharedDirectives = angular.module("sharedDirectives", []);


sharedDirectives.directive("interestPoints", function () {

    function link($scope, element, attributes, controller ) {

       $(element).find('#interest-points').owlCarousel({
           items : 4, //4 items above 1200px browser width
           itemsDesktop : [1200,3], //3 items between 1200px and 992px
           itemsDesktopSmall : [992,3], // betweem 992px and 768px
           itemsTablet: [850,2], //1 items between 768 and 0
           itemsMobile : [600,1] // itemsMobile disabled - inherit from itemsTablet option

       });
    }

    return {
        restrict: "E",
        templateUrl : "../html/views/interest-points.html",
        link: link,
        scope: {
            interestPoints: '@'
        }

    };
});

息points.html:

<div id="interest-points" class="owl-carousel">
    <div ng-repeat="point in interestPoints" class="item">
        <img ng-src="{{point.url}}" alt="Owl Image"><h4>27<br>JUL</h4>

    </div>
</div>

home.html的:

<div ng-controller='homeController'>
<interest-points interest-points="{{points}}""></interest-points>
</div>

我嘗試通過$ index進行跟蹤,但未出現錯誤並且它不會迭代

您正在使用interestPoints: '@'作為將interestPoints綁定到合並范圍的方法。 實際上,這僅將字符串{{points}}綁定到interestPoints而不是實際在父級范圍內評估該表達式。

使用interestPoints: '='作為綁定方法,然后使用interest-points="points"獲得所需的行為。

指令定義對象下的相關文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM