html设置%3cdiv%3e竖直,加载html后的角度引导

本文介绍如何在Angular应用中动态加载外部HTML文件并将其编译为Angular-bound视图。通过使用$compile服务,我们可以实现这一目标。具体步骤包括设置控制器、获取HTML内容、编译HTML并链接到指定的作用域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我会回应别人提到的内容:这种事情通常是一个坏主意,但我也明白,你有时不得不以不喜欢的方式使用旧代码.所有这一切,您可以将从Angular外部加载的HTML加载到

the $compile service的Angular-bound视图中.以下是您可以重写当前示例以使其与$compile一起工作的方式:

// We have to set up controllers ahead of time.

myApp.controller('AfterCtrl', function($scope) {

$scope.loaded = 'Is now loaded';

});

//loads html and afterwards creates a controller

$('button').on('click', function() {

$.get('ajax.html', function(data) {

// Get the $compile service from the app's injector

var injector = $('[ng-app]').injector();

var $compile = injector.get('$compile');

// Compile the HTML into a linking function...

var linkFn = $compile(data);

// ...and link it to the scope we're interested in.

// Here we'll use the $rootScope.

var $rootScope = injector.get('$rootScope');

var elem = linkFn($rootScope);

$('.content').append(elem);

// Now that the content has been compiled, linked,

// and added to the DOM, we must trigger a digest cycle

// on the scope we used in order to update bindings.

$rootScope.$digest();

}, 'html');

});

如果您可以将功能构建为指令而不是使用原始的jQuery,那么可以简化事情 – 您可以向其中注入$compile和$rootScope服务,甚至可以在指令中使用本地范围.如果您可以将动态绑定用于< ng-include>元素代替

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值