【问题标题】:AngularJS ng-bind-html not working in <ng-view> pagesAngularJS ng-bind-html 在 <ng-view> 页面中不起作用
【发布时间】:2015-09-04 07:19:25
【问题描述】:

在母版页中 n-bind-html 正在工作,但是在调用视图页面时没有得到任何数据。

ng-bind-html="home.title" 正在索引标题 div 中工作。

index.html

             ....

             <title ng-bind-html="home.title"></title>

             ....

            <ng-view></ng-view>

contact.html 但 ng-view 联系页面 ng-bind-html="home.title" 不起作用,{{contact.info}} 不起作用。

            <h4 ng-bind-html="home.title"></h4>
            <p>{{contact.info}}</p>

控制器:

    $scope.trustedHtml = function (plainText) {
        return $sce.trustAsHtml(plainText);
    }
    $http.get('admin/api/?s=getAbout&lang='+lang).success(
        function(r){
                $scope.about = r.text;
                //$scope.about = r.email;
        }
    );


    $http.get('admin/api/?s=getContact'+lang).success(
        function(r){
                $scope.contact = r;
                //$scope.contacttext.infoH = r[0].infoH;
                //$scope.contact.infoH = $sce.trustAsHtml(r.infoH); 

        }
    );


    $http.get('admin/api/?s=getHome&lang='+lang).success(
        function(r){
                $scope.home = r;
                $scope.home.address = $sce.trustAsHtml(r.address); 
                $scope.home.phone = $sce.trustAsHtml(r.phone); 
                $scope.home.email = $sce.trustAsHtml(r.email);
                $scope.home.title = $sce.trustAsHtml(r.title);                  
        }
    );

关于 about 的 ng-view 页面正在运行。 about.html

       <div class="container" style="font-size:16px;" ng-bind-html="trustedHtml(about)"></div>

【问题讨论】:

  • 后控制器代码,路由有不同的作用域,如果home.title在页面作用域内对路由作用域是不可见的

标签: angularjs


【解决方案1】:

您需要为动态添加的元素添加以下代码代码。 See Here 。希望这会有所帮助:

AngularApp.run(function($rootScope, $location, $timeout) {
    $rootScope.$on('$viewContentLoaded', function() {
        $timeout(function() {
            componentHandler.upgradeAllRegistered();
        });
    });
});

How Component Handle works

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2019-01-06
    • 2018-06-16
    相关资源
    最近更新 更多