我的角度控制器中有一个函数,我希望这个函数可以在文档准备好的情况下运行但我注意到角度在创建dom时运行它.
function myController($scope)
{
$scope.init = function()
{
// I'd like to run this on document ready
}
$scope.init(); // doesn't work, loads my init before the page has completely loaded
}
Run Code Online (Sandbox Code Playgroud)
谁知道我怎么能这样做?
我有一个函数,我想在加载页面内容后调用.我读到了$ viewContentLoaded,它对我不起作用.我正在寻找类似的东西
document.addEventListener('DOMContentLoaded', function () {
//Content goes here
}, false);
Run Code Online (Sandbox Code Playgroud)
以上调用在AngularJs控制器中对我不起作用.
在我的Angular组件视图初始化为将数值转换为星形后,我需要运行一些jQuery代码.
我把代码放在ngAfterViewInit函数中,但它没有做我需要的东西.我在函数中设置了一个断点,我发现在调用ngAfterViewInit函数时,我的组件的大部分HTML都没有加载.未加载的部分使用*ngFor指令生成.
在该指令生成了我需要操作的所有html之后,如何让我的代码运行?
使用AngularJS,是否可以使用"onload"参数来触发在"子"控制器(由包含的模板调用的控制器)中定义的函数?
例:
<!-- parent container -->
<div ng-include="'/path/template.html'" onload="childOnLoad()"></div>
<!-- template.html -->
<div ng-controller="childController">
<p ng-bind="txt"></p>
</div>
<!-- childController.js -->
app.controller('childController', function($scope) {
$scope.txt = "Test text";
$scope.childOnLoad = function() {
alert("Loaded!");
};
});
Run Code Online (Sandbox Code Playgroud)
是否有意义?或者我应该简单地调用childController中的函数,如下所示?
<!-- childController.js -->
app.controller('childController', function($scope) {
$scope.txt = "Test text";
$scope.childOnLoad = function() {
alert("Loaded!");
};
$scope.childOnLoad();
});
Run Code Online (Sandbox Code Playgroud) 我正在学习AngularJS.我有一些文章标签,点击一个按钮,每个文章页面都显示没有任何页面刷新.这是一个页面的网站.我想要的是当文章ID"showSelector"被加载时我想调用myFunction(),在这个函数中我想显示一个警告.但警报没有显示.
我怎样才能做到这一点?
<article id="showSelector" class="panel" ng-controller="CinemaCtrl" onload="myFunction()">
<header>
<a ng-click="back()" class="icon fa-arrow-circle-left"></a><h2>Shows in {{getSelectedCinema()}}</h2>
</header>
<p>
These shows are played in our single room theatre. Select one to reserce a ticket for it.
</p>
<section>
<div class="row">
<div class="4u" ng-repeat="show in shows">
<div class="movieCard">
<a ng-click="selectShow(show)"></a>
<h3>{{show.nameOfShow}}</h3>
<h4>{{show.timeOfShow | date:'MMM d'}}</h4>
<h4>{{show.timeOfShow | date:'HH:mm'}}</h4>
<p>Free seats: {{show.reservations | freeSeatFilter}}</p>
</div>
</div>
</div>
</section>
<script>
function myFunction() {
alert("Page is loaded");
};
</script>
</article>
Run Code Online (Sandbox Code Playgroud)