我正在学习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>
您应该从控制器调用此函数。
angular.module('App', [])
.controller('CinemaCtrl', ['$scope', function($scope) {
myFunction();
}]);
即使使用普通的javascript / html,您的函数也不会在页面加载时运行,因为您所做的全部工作就是定义函数,因此您永远不会调用它。这实际上与angular没有任何关系,但是由于您正在使用angular,因此上述内容将成为调用函数的“角度方式”。
显然最好还是在控制器中声明功能。
编辑:实际上,我看到您的“加载”-当角度将HTML注入DOM时不会被调用。html永远不会“加载”(或页面仅加载一次)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句