简体   繁体   English

在角度模板中将HTML页面添加到div

[英]Add Html page to div in angular template

I have angular template , which has a div. 我有角模板,其中有一个div。 I am trying to load html view ( .html ) page to the div based on a $watch. 我试图基于$ watch将html视图(.html)页面加载到div。 The view are loaded properly, but not attached to $scope object. 视图已正确加载,但未附加到$ scope对象。 Here is my controller , I am only posting the part of the code that loads the html view. 这是我的控制器,我只发布了加载html视图的代码部分。

var filtertemplate = "#locationChart_right";
$scope.templateUrl = '/_layouts/AngularControls/MyController/Views/MyChart.html';
$scope.$watch("currentItem", function () {

$scope.currentConfig = $rootScope.currentItem;
LocDetailsChartService.getUserPrefs()
.then(function (response) {
    var data = response.data.ChartsUserPrefs;
    $scope.MeritType = data.MeritType;
    if ($scope.MeritType !== undefined) {
        if ($scope.MeritType == "Score") {
            $(filtertemplate).load("/_layouts/AngularControls/MyController/Views/MyScoreChart.html");
            $scope.$apply()
        }
        if ($scope.MeritType == "Potential") {
            $(filtertemplate).load("/_layouts/AngularControls/MyController/Views/MyPercentChart.html");
            $scope.$apply()
        }
    }
   // $scope.radioButtonHandler($scope.MeritType);
});
});

HTML HTML

Can anybody suggest me where I am doing the mistake ? 有人可以建议我在哪里做错吗?

Well, you are mixing jQuery with Angular where it seems you shouldn't be. 好吧,您正在将jQuery与Angular混合使用,看来您不应该这样做。

You can dynamically load html templates into your page in a much easier manner using ng-include. 您可以使用ng-include以更简单的方式将html模板动态加载到页面中。

Lets say you have a div 假设您有一个div

<div id='locationChart_right'></div> <!-- traditionally you use an id to find this div and replace the content... the jQuery way -->

but with Angular, you can just assign a variable with ng-include to change the template. 但是使用Angular,您只需使用ng-include分配变量即可更改模板。

<div ng-include="templateToLoad"></div>

now, in your JS, all you need is a $scope variable to assign which template to load. 现在,在您的JS中,您只需要一个$ scope变量来分配要加载的模板。

LocDetailsChartService.getUserPrefs()
.then(function (response) {
    var data = response.data.ChartsUserPrefs;
    $scope.MeritType = data.MeritType;
    if ($scope.MeritType !== undefined) {
        if ($scope.MeritType == "Score") {
            $scope.templateToLoad =  "/_layouts/AngularControls/MyController/Views/MyScoreChart.html";
        }
        if ($scope.MeritType == "Potential") {
            $scope.templateToLoad = "/_layouts/AngularControls/MyController/Views/MyPercentChart.html";
        }
    }
});

This will automatically load the proper template and update scope objects in your view. 这将自动加载适当的模板并更新视图中的范围对象。

IF you need to keep the jQuery way for whatever reason, you will need to force a $compile on the element to update the $scope values. 如果出于任何原因需要保留jQuery方式,则需要在元素上强制执行$ compile以更新$ scope值。

like so: 像这样:

$compile($(filtertemplate)($scope);

In Context: 在上下文中:

if ($scope.MeritType == "Potential") {
    $(filtertemplate).load("/_layouts/AngularControls/MyController/Views/MyPercentChart.html");
        $compile($(filtertemplate))($scope);
    }
}

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

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