What you're trying to do is transclude content into the directive. To do that you need to add a transclude
property and an ng-transclude
tag into your template.
Also, the {{test}}
you're seeing there is in the controller's scope, NOT the directives scope. The directive's scope is in the template|templateUrl
property of the directive.
Since the {{test}}
is in the controller scope, you need to add an actual test
property to the controller scope in order for it to work.
http://plnkr.co/edit/nCZqjHqm9VQqISOnCdNu?p=info
'use strict';
angular.module('myApp', [])
.directive('infoDirective', function(){
return {
scope: { test: '='},
controller: function($scope){console.log("array-info", $scope); },
template: '<ng-transclude></ng-transclude>', //infoDirective $scope
transclude: true
};
});
angular.module('myApp')
.controller('myCtrl', function($scope) {
$scope.test = 'alpha';
console.log($scope);
});
<div class='row'>
<div class='col-md-12'>
<h1 >Test</h1>
<div info-directive test="test">
Variable test = {{ test }} <!-- myCtrl $scope -->
</div>
</div>
</div>
If you'd like to show the test
property of the directive scope, move the content between the directive's tag to the template
of the directive:
http://plnkr.co/edit/sKCz3OpVPTZP9OJb0U8d?p=preview
'use strict';
angular.module('myApp', [])
.directive('infoDirective', function(){
return {
scope: { test: '='},
controller: function($scope){console.log("array-info", $scope); },
template: 'Variable test = {{ test }}'
};
});
angular.module('myApp')
.controller('myCtrl', function($scope) {
console.log($scope);
});
EDIT: Naturally you can also use combine a directive with a template and transclusion.
http://plnkr.co/edit/QviGc8w2M97ZhU8iyDjj?p=preview
'use strict';
angular.module('myApp', [])
.directive('infoDirective', function(){
return {
scope: { test: '='},
controller: function($scope){console.log("array-info", $scope); },
template: '<div>Variable test = {{test}} in infoDirective</div><ng-transclude></ng-transclude>',
transclude: true
};
});
angular.module('myApp')
.controller('myCtrl', function($scope) {
$scope.test = 'alpha';
console.log($scope);
});
<div info-directive test="test">Variable test = {{ test }} from myCtrl</div>