angular.js - AngularJS 中如何给一个自定义指令 directive 传递一个对象类型的参数?
迷茫
迷茫 2017-05-15 17:03:23
0
1
928

我想写一个自定义指令,根据外部传进来的数组,在页面上生成一个表格。我该怎么做?angularjs的自定义指令是否只能接收字符串参数?哪位大神能帮帮忙,谢谢!

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

membalas semua(1)
巴扎黑

Saya boleh memberi anda contoh mudah: jsFiddle

<p ng-controller="DemoCtrl">
      <ng-table data="list"></ng-table>
</p>
var demo = angular.module('demo', []);

demo.directive('ngTable', function(){

    return {
        restrict: 'E',
        scope: {
            data: '='
        },
        link: function($scope, element, attrs){
            
        },
        template: '<table><tr ng-repeat="item in data"><td>{{ item.id }}</td><td>{{ item.name }}</td></tr></table>'
    };
});


demo.controller('DemoCtrl', function($scope){
    $scope.list = [
        {
            id: 123,
            name: 'Hello World'
        },{
            id: 234,
            name: 'Fucking world'
        },{
            id: 345,
            name: 'What did you say?'
        }
    ];
});

Mengenai sama ada "hanya rentetan boleh dilalui", anda perlu membaca dokumentasi terlebih dahulu:

Alamat dokumen: skop

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan