简体   繁体   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);
                    });
                }; 
            }
        };
    });

I have a partial page trying to compile the page is working just showing the template as such. 我有一个试图编译页面的部分页面,仅显示模板本身。

Plunkr is avaliable here Plunkr在这里有售

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

Issue found i was binding the html not the compiled object resolved the issue like below 发现我绑定了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());
                    });
                }; 
            }
        };
    });

I haven't seen anyone try to load a template this way before. 我还没见过有人尝试过以这种方式加载模板。 The proper Angular ways of loading a template are the following: 正确的Angular加载模板的方法如下:

  • Using ng-view to load a predefined template file. 使用ng-view加载预定义的模板文件。
  • Building your own custom directive. 建立自己的自定义指令。

I feel like you are attempting to do the second method. 我觉得您正在尝试执行第二种方法。 The following code sample is an example of how to do that. 下面的代码示例是如何执行此操作的示例。

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

In your template.html file you would have your template you wish to load. 在template.html文件中,您将希望加载模板。

For much more detail and examples on how to build your own custom directive elements, see the section Writing Directives (long version) in the Angular docs. 有关如何构建自己的自定义指令元素的更多详细信息和示例,请参阅Angular文档中的“ 编写指令(长版) ”部分。

UPDATE: Edited to show example of templateUrl. 更新:编辑以显示templateUrl的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM