Answer the question
In order to leave comments, you need to log in
Why can't the angular directive see the variable?
var app = angular.module('app', [
'templates'
]);
(function (){
app
.controller("templateCtrl", ["$scope", function($scope){
$scope.templatePath = "registration/form";
this.changeTemplate = function(path) {
$scope.templatePath = path;
};
}])
.directive("viewer", function(){
return {
template: "Path: " + templatePath,
}
});
})();
<body>
<div class="container-fluid" ng-controller="templateCtrl">
<viewer></viewer>
</div>
<script src="libs/libs.min.js"></script>
<script src="js/main.min.js"></script>
</body>
Answer the question
In order to leave comments, you need to log in
By itself, the directive should not see $scope.templatePath in this way. As I understood from what you need:
// ...
template: "Path: {{ templatePath }}"
// ...
And how to throw the template variable into the directive's template property? It gives me html text.
app
.controller("templateCtrl", function ($scope, $templateCache){
$scope.templatePath = "registration/form";
$scope.template = $templateCache.get($scope.templatePath);
})
.directive("templateview", function (){
return {
scope: {
template: "="
},
restrict: "E",
template: "{{template}}",
link: function (scope){
console.log(scope);
}
}
});
<templateview data-template="template"></templateview>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question