簡體   English   中英

局部視圖無法以正確的角度編譯

[英]Partial View does not compile properly in angular

// Code goes here
var mymodule = angular.module('myapp', []);
mymodule.controller('mycontroller', function ($scope) {

});

mymodule.directive('pvTempUrl',
    function ($http, $compile, $log, $templateCache) {
        $log.info("Directive Called");
        return {
            restrict: 'A',
            replace: true,
            compile: function (telement, tattr, transclude) {
                var templateloader = $http.get(tattr.pvTempUrl, { cache: $templateCache }).
                    success(function (data) {
                        $log.info("Success-" + data);
                        telement.html(data);
                    }).
                    error(function (data, status) {
                        $log.warn("Error occured - " + data + " status-" + status);
                    });
                return function (scope, element, attr) {
                    templateloader.then(function () {
                        var compiledHtm = ($compile(telement.html())(scope)).html();
                        $log.info("compiled html-" + compiledHtm);
                        element.html(compiledHtm);
                    });
                }; 
            }
        };
    });

我有一個試圖編譯頁面的部分頁面,僅顯示模板本身。

Plunkr在這里有售

http://plnkr.co/edit/U85rmXhuQGKx5pkzUu99?p=preview

發現我綁定了html而不是編譯對象的問題解決了如下問題

// Code goes here
var mymodule = angular.module('myapp', []);
mymodule.controller('mycontroller', function ($scope) {

});

mymodule.directive('pvTempUrl',
    function ($http, $compile, $log, $templateCache) {
        $log.info("Directive Called");
        return {
            restrict: 'A',
            replace: true,
            compile: function (telement, tattr, transclude) {
                var templateloader = $http.get(tattr.pvTempUrl, { cache: $templateCache }).
                    success(function (data) {
                        $log.info("Success-" + data);
                        telement.html(data);
                    }).
                    error(function (data, status) {
                        $log.warn("Error occured - " + data + " status-" + status);
                    });
                return function (scope, element, attr) {
                    templateloader.then(function () {
                        var compiledHtm = ($compile(telement.html())(scope));
                        $log.info("compiled html-" + compiledHtm);
                        //element.html(compiledHtm);
                        element.replaceWith(compiledHtm);
                         $log.info(element.html());
                    });
                }; 
            }
        };
    });

我還沒見過有人嘗試過以這種方式加載模板。 正確的Angular加載模板的方法如下:

  • 使用ng-view加載預定義的模板文件。
  • 建立自己的自定義指令。

我覺得您正在嘗試執行第二種方法。 下面的代碼示例是如何執行此操作的示例。

angular.module("myapp")
.directive('buttonsRadio', function() {
return {
    restrict: 'E',
    scope: false,
    controller: function($scope){
        //Any logic required for directive to work.    
    },
    templateUrl: 'template.html'
};
})

在template.html文件中,您將希望加載模板。

有關如何構建自己的自定義指令元素的更多詳細信息和示例,請參閱Angular文檔中的“ 編寫指令(長版) ”部分。

更新:編輯以顯示templateUrl的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM