Why variable defined in the directive controller not working?

≯℡__Kan透↙ 提交于 2019-12-13 07:16:35

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/34893982/why-variable-defined-in-the-directive-controller-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!