将数组对象从控制器传递到AngularJS中的自定义指令

时间:2022-07-13 15:10:25

Im trying to pass a array of objects from a angular controller to a custom directive element and iterate the object with ng-repeat, but appears the following error: [ngRepeat:dupes]

我试图将一个对象数组从一个角度控制器传递给一个自定义指令元素,并使用ng-repeat对对象进行迭代,但出现以下错误:[ngRepeat:dupes]

home.js:

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:

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: '@'
        }

    };
});

interest-points.html:

interest-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:

home:

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

I tried with track by $index but the error don't appear and it don't iterate

我尝试使用$index跟踪,但是错误不会出现,也不会迭代

1 个解决方案

#1


3  

You are using interestPoints: '@' as the method of binding interestPoints to the scope. That actually binds only the string {{points}} to interestPoints instead of actually evaluating that expression in the parent's scope.

您正在使用“@”作为将兴趣点绑定到范围的方法。实际上,它只将字符串{{{{{point}绑定到兴趣点,而不实际计算父范围中的表达式。

Use the interestPoints: '=' as the binding method and then interest-points="points" to get the desired behaviour.

使用兴趣点:'='作为绑定方法,然后兴趣点="points"来获得所需的行为。

Related docs under the heading Directive definition object.

标题下的相关文档指令定义对象。

#1


3  

You are using interestPoints: '@' as the method of binding interestPoints to the scope. That actually binds only the string {{points}} to interestPoints instead of actually evaluating that expression in the parent's scope.

您正在使用“@”作为将兴趣点绑定到范围的方法。实际上,它只将字符串{{{{{point}绑定到兴趣点,而不实际计算父范围中的表达式。

Use the interestPoints: '=' as the binding method and then interest-points="points" to get the desired behaviour.

使用兴趣点:'='作为绑定方法,然后兴趣点="points"来获得所需的行为。

Related docs under the heading Directive definition object.

标题下的相关文档指令定义对象。