为什么指令控制器中定义的变量不起作用? [英] Why variable defined in the directive controller not working?

查看:24
本文介绍了为什么指令控制器中定义的变量不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我在控制器中定义了 'sayHi',它没有显示在模板中?

js:

(function(){var app = angular.module('myApp', []);app.directive("directive1", function(){返回 {限制:'E',范围: {},链接:函数($scope){},控制器:['$scope',函数($scope){$scope.sayHi = '嗨';window.console.log($scope.sayHi);}]};});})();

html:

<div style="border: 1px solid; padding: 10px; min-height: 100px;">指令1:<指令1>{{打招呼}}</directive1>

详细 plnkr

解决方案

您的指令没有任何模板.您需要添加

模板:'{{ sayHi }}',

到您的指令定义. 中的内容不构成指令的模板.

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

解决方案

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.

这篇关于为什么指令控制器中定义的变量不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆