简体   繁体   中英

Angular Template directive for ng-repeat and ng-class

iam trying to get the code in a Element Template:

rent.html

<td>{{ rent.id }}</td>
<td>{{ rent.auto }}</td>
<td>{{ rent.person }}</td>
<td>{{ rent.title }}</td>
<td>{{ rent.start }}</td>
<td>{{ rent.end }}</td>
<td><a ng-href="#" ng-click="acceptRent(rent.id)"><img src="bundles/chriskfzbuchung/images/accept.png" width="15" ng-hide="rent.buchungsStatus == 1"></a></td>
<td><a ng-href="#" ng-click="declineRent(rent.id)"><img src="bundles/chriskfzbuchung/images/decline.png" width="15" ng-hide="rent.buchungsStatus == 2"></a></td>

controller.js

 kfzModule.directive("kfzRent", function(){
        return {
            restrict: 'E',
            templateUrl: '/kfz-buchung/rent.html'
        };
    });

overview.html

<tr kfz-rent ng-repeat="rent in rents" ng-class="{'success' : rent.buchungsStatus == 1, 'danger' : rent.buchungsStatus == 2}">
                </tr>

I dont know how to deal with the rest in overview.html.

I finally want just an <kfz-rent></kfz-rent> .

Thanks!

This should work:

 <kfz-rent ng-repeat="rent in rents" ng-class="{'success' : rent.buchungsStatus == 1, 'danger' : rent.buchungsStatus == 2}">
    <td>{{ rent.id }}</td>
    <td>{{ rent.auto }}</td>
    <td>{{ rent.person }}</td>
    <td>{{ rent.title }}</td>
    <td>{{ rent.start }}</td>
    <td>{{ rent.end }}</td>
    <td><a ng-href="#" ng-click="acceptRent(rent.id)"><img src="bundles/chriskfzbuchung/images/accept.png" width="15" ng-hide="rent.buchungsStatus == 1"></a></td>
    <td><a ng-href="#" ng-click="declineRent(rent.id)"><img src="bundles/chriskfzbuchung/images/decline.png" width="15" ng-hide="rent.buchungsStatus == 2"></a></td>
</kfz-rent>

The code for ng-repeat seems to be fine. But you have to create the array in your controller:

$scope.rents = []; 

Dont forget to make a alias for your controller

kfzModule.directive("kfzRent", function(){
    return {
        restrict: 'E',
        templateUrl: '/kfz-buchung/rent.html'
    };
   },
   controllerAs: 'rentController'
 };
});

Cheers,

Valentin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM