ng-cloak无效

时间:2016-02-03 06:26:21

标签: javascript angularjs

我编写了一段代码,该代码需要输入一些文字,并删除了#'#'文本中的标签。代码运行正常。但最初当页面加载时我得到了{{textWithHashes | textWithoutDashes}}'。这不是很吸引人。我尝试过使用ng-cloak但我仍然无法隐藏它。有人可以告诉我为什么它不起作用,以及是否有更好的方法来隐藏它。

Html文件:

<!DOCTYPE html>
<html>
    <head>
        <title>Removing the delimiters from the Text</title>
        <script src="js/angular.min.js"></script>
        <script src="js/appModule1.js"></script>
    </head>

    <body ng-app="appModule" ng-controller="appModuleCtrl">

        <input type="text" ng-model="textWithHashes">

        <!--The next line will give the text without the hash tags-->

        <label ng-cloak>{{textWithHashes | textWithoutDashes}}</label>

    </body>
</html>

Javascript文件:

var appModule=angular.module('appModule',[]);

appModule.filter('textWithoutDashes',function(){
    return function(text){
        return text.split('#').join(' ');
    }
});


appModule.controller('appModuleCtrl',function(){

});

2 个答案:

答案 0 :(得分:0)

您可以使用ng-bind并过滤控制器中的文本(这也是推荐的性能wize):

<label ng-bind="textWithHashes"></label>

在你的控制器中:

appModule.controller('appModuleCtrl',function($scope, $filter){
    $filter('textWithoutDashes')($scope.textWithHashes);
});

答案 1 :(得分:0)

根据ng-cloak文档(https://docs.angularjs.org/api/ng/directive/ngCloak),您必须在头标记中添加此css:

[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
  display: none !important;
}
相关问题