Search code examples
angularjsangularjs-scopeangular-directive

Why variable defined in the directive controller not working?


Why i defined 'sayHi' in the controller, and it does not display in the template?

js:

(function(){
var app = angular.module('myApp', []);

app.directive("directive1", function(){
    return {
        restrict : 'E',
        scope: {

        },
        link : function($scope){        

        },

        controller: ['$scope', function($scope){
          $scope.sayHi = 'hi';
          window.console.log($scope.sayHi);
        }]

    };  
});
})();

html:

<div style="border: 1px solid; padding: 10px; min-height: 100px;">
Directive1 :   
<directive1>
    {{sayHi}}
</directive1>
</div>

detail plnkr


Solution

  • Your directive doesn't have any template. You need to add

    template: '{{ sayHi }}',
    

    To your directive definition. The content inside the <directive1></directive> does not constitute the template of the directive.