我想写一个自定义指令,根据外部传进来的数组,在页面上生成一个表格。我该怎么做?angularjs的自定义指令是否只能接收字符串参数?哪位大神能帮帮忙,谢谢!
业精于勤,荒于嬉;行成于思,毁于随。
I can give you a simple example: 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?' } ]; });
As for whether "only strings can be passed", you need to read the documentation first:
Document address: scope
I can give you a simple example: jsFiddle
As for whether "only strings can be passed", you need to read the documentation first:
Document address: scope