嗨,大家好,我需要了解在ngRoute加载templateURL之后如何加载脚本。
在页面加载templateUrl文件html之后,我需要实例化两个滑块。
我使用angularJs和ngRoute。我认为控制器是在HTML完成加载模板文件之前执行的。
我有此代码:
app.config(function($routeProvider){
//set route
$routeProvider
.when('/', {
templateUrl: 'page/configurator.html',
controller: 'mainController'
})
.otherwise({
redirectTo:'/'
})
});
app.controller('mainController', function($scope){
//data for main=configurator controller
//$scope.message="Everyone come and see how good I look! i am in MAIN=configurator page";
$scope.pageTitle="SELECT THE ICE TYPES AND MAX DAILY PRODUCTS";
alert("test");
$(window).trigger("resize");
});
将html插入页面后,我该为运行脚本做什么?
谢谢
虚拟现实
最佳答案
后链接功能是指令的渲染周期的最后一个阶段,它也从最里面的元素到最外面的元素被调用。因此,您可以在顶层元素上使用它来检测何时angular渲染了所有子指令(ng或其他)。
app.directive('onFinishRender', function ($timeout,$rootScope) {
return {
restrict: 'A',
link: function (scope, element, attr) {
$timeout(function () {
$rootScope.$broadcast("hasFinishedRender");
});
}
}
})
app.controller('mainController', function($scope){
$scope.$on('hasFinishedRender',function(){
// load script dynamically using any method
var my_awesome_script = document.createElement('script');
my_awesome_script.setAttribute('src','http://example.com/site.js');
document.head.appendChild(my_awesome_script);
});
});
您可能还需要一些逻辑来防止它在将来的摘要周期中多次触发。还假设角度为1.x