如何将数据传递到ng-repeat表中的指令

时间:2015-05-20 19:23:31

标签: angularjs angularjs-directive

现在我有一些在某些地方使用的指令,在一个地方我需要加载数据来启动它们(如果没有数据,请保持空白)。以下是其中一个指令的模板:

<div class="col-lg-12" style='z-index: 99998;'>
    <span class="col-lg-12 col-md-12 right-inner-addon typeAhead">
        <i class="glyphicon glyphicon-search" ng-show="isBillToEmpty"></i> <i  ng-click="cleanBillTo()"
        class="glyphicon glyphicon-close" ng-show="!isBillToEmpty" ></i></a> <input class="data  "
        id="billTo" ng-model="billTo"
        typeahead=" billTos.ID as billTos.VALUE  for billTos in getBillTos($viewValue)"
        typeahead-input-formatter="formatBillToText($model)" type="text">
    </span>
</div>
<div class="col-lg-12">
    <span class="col-md-12"> <label id="labelBillTo" for="billTo">{{billToLabel}}</label>
    </span>
</div>

DirectiveJS:

function($scope, BillToService) {
                    $scope.billTos = {};
                    $scope.billTo = ''; 
                    $scope.billToLabel = 'Bill To'
                    $scope.isBillToEmpty = true;
                    $scope.formatBillToText = function(model) {
                        for (var i = 0; i < $scope.billTos.length; i++) {
                            if (model === $scope.billTos[i].ID) {
                                return $scope.billTos[i].VALUE;
                            }
                        }

                    }
                    $scope.$watch('billTo', function() {
                        $scope.billToID = $scope.billTo;
                        if($scope.billTo.length > 0){
                            $scope.isBillToEmpty = false;
                        }else{
                            $scope.isBillToEmpty = true;
                        }
                    });
                    $scope.cleanBillTo = function() {

                        $scope.billTo = "";
                        $scope.billToID = "";
                    };
                    $scope.billTos = [{'ID':$scope.billToID,'VALUE':$scope.initialBillToText}];
                    $scope.billTo = $scope.billToID;
                    $scope.getBillTos = function(billToText) {

                        var promise = BillToService.getBillTos(billToText,
                                $scope.user.expenServerID);
                        promise.then(function(billTosInformation) {
                            $scope.billTos = billTosInformation;

                        });
                        $scope.billTos = BillToService.getBillTos(billToText,
                                $scope.user.expenServerID);
                        return $scope.billTos;                  
                    };
                } ]).directive('smartBillTo', function() {
    return {
        restrict : 'E',
        controller : "BillToCtrl",
        templateUrl : 'components/directives/billTo/bill-to.html'
    }



<table id='AllocationTable'  ng-controller='maincontroller' class="table table-hover">
    <thead>
        <tr>
            <th class="text-left">
                <input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" />
            </th>
            <th class="text-center">Charge To Dept
            </th>
            <th class="text-center">Sub Account
            </th>
            <th class="text-center">Activity
            </th>
            <th class="text-center">Category
            </th>
            <th class="text-center">Bill To
            </th>
            <th class="text-center">Expense Item
            </th>
            <th class="text-center">Percentage
            </th>
        </tr>
    </thead>
    <tbody>
    <tr ng-repeat='data in datalist'>*How can I pass data to <smart-billTo>*
        <td class="text-left">
            <input type="checkbox" ng-model=''/>
        </td>
        <td class="text-center"><smart-department ></smart-department></td>
        <td class="text-center"><input type='text' ng-model=''/></td>
        <td class="text-center"><input type='text' ng-model=''/></td>
        <td class="text-center"><input type='text' ng-model=''/></td>
        <td class="text-center"><smart-bill-to></smart-bill-to></td>
        <td class="text-center"><smart-item ></smart-item></td>
        <td class="text-center"><input type='text' ng-model=''/></td>
    </tr>
    </tbody>
</table>

在maincontroller中,我从service获取数据到init指令值:

$scope.datalist = Someservice.getdata();

如何将数据传递给指令以初始化输入值(例如bill-to)。

0 个答案:

没有答案