angularjs pass newly created array in attribute to directive

后端 未结 3 1999
余生分开走
余生分开走 2020-12-31 10:17

I\'ve created this fiddle to show my issue...

http://jsfiddle.net/dQDtw/

I\'m passing a newly created array to a directive, and everything is working out jus

3条回答
  •  忘掉有多难
    2020-12-31 10:53

    That error is because your directive is not able to interpret the array as an array, Try this:

    
         
    
    
    
    
    
    var myApp = angular.module('myApp', []);
    
    myApp.directive('testDir', function() {
                    return { restrict: 'E'
                           , scope: { famPeople: '=' }
                           , template: "
    1. {{p}}" }; });

    Controller and directive:

    myApp.controller("ctrl1",function($scope){
    $scope.people=[1,4,6];
    });
    

    EDIT

    or you could pass it in as an attribute and parse it to an array:

    
         
    
    
    

    Directive:

    var myApp = angular.module('myApp', []);
    
    myApp.directive('testDir', function() {
                    return { restrict: 'E', 
                            //scope: { famPeople: '=' },
                           template: "
    1. {{p}}", link:function(scope, element, attrs){ scope.people=JSON.parse(attrs.famPeople); } }; });

    See fiddle.

提交回复
热议问题