I'm working my way through angular js, and wrestling with ng-repeat and directives. It looks ng-repeat symbols aren't expanded in the attributes of directives. What I'd like to do is take two arrays ["a1", "a2", "a3"]
and ["b1", "b2", "b3"]
My markup looks like this:
<div ng-app="app">
<div ng-controller="TestController">
<div ng-repeat="a in as">
<h2>{{a}}</h2> <!-- Works fine. -->
<directive-test a="test" b="test"></directive-test>
<div ng-repeat="b in bs">
<h3>{{b}}</h3> <!-- Works fine. -->
<directive-test a="{{a}}" b="{{b}}"></directive-test><!-- Does not work. -->
</div>
</div>
</div>
</div>
and the backing javascript looks like this:
var app = angular.module("app", []);
app.controller('TestController', ['$scope', '$window', function ($scope, $window) {
$scope.as = ['a1', 'a2', 'a3'];
$scope.bs = ['b1', 'b2', 'b3'];
}]);
app.directive('directiveTest', function () {
return {
restrict: "E",
replace: true,
transclude: true,
scope: true,
template:
'<div>{{a}} by {{b}}</div>',
controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {
$scope.a = $attrs.a;
$scope.b = $attrs.b;
}]
};
});
I'd expect the {{a}}
and {{b}}
to be expanded and see output like "a1 by b1", but instead, I see output like {{a}} by {{b}}
. Is there a way to expand ng-repeat elements to use as attributes of custom directives?