[英]how to execute a function when loading a page in Angularjs
Best regards, I have a problem with a component, which is divided into app.component and its respective .html, I need a function to be executed when loading the page, when loading the modal that I have in this component, I tried with $ (document) .ready ()
, but it didn't work, I think it's a problem in the way I make the call, I don't understand very well how it still works,最好的问候,我有一个组件有问题,它分为app.component及其各自的.html,我需要在加载页面时执行一个函数,在加载我在这个组件中的模态时,我试过
$ (document) .ready ()
,但它没有用,我认为这是我打电话的方式有问题,我不太明白它是如何工作的,
<div class="row">
<div class="col-md-12" style="text-align:center;">
<a class="boton grande intermedio" ng-touchstart="$ctrl.listConve()">Validar</a>
</div>
</div>
but I need to load it in this select:但我需要在这个选择中加载它:
<div class="row">
<div class="form-group" style="text-align:center;">
<select class="form-control" id="convenio" name="selectConvenio"
placeholder="Convenio" >
<option selected>Elegir Convenio...</option>
<option ng-repeat="option in $ctr.dataList">{{$option.nombreConvenio}}</option>
</select>
</div>
</div>
intente llamarlo de esta forma: Inte llamarlo de esta forma:
<script>
$(document).ready(
function(){
alert('funciona');
$ctrl.listConve()
})
</script>
But it didn't work either.但它也没有奏效。
Use the $onInit
life-cycle hook in the controller:在控制器中使用
$onInit
生命周期钩子:
app.controller("ctrl", function() {
this.listConve = function() {
//...
};
this.$onInit = function() {
this.listConve();
};
})
From the Docs:从文档:
Life-cycle hooks
生命周期钩子
Directive controllers can provide the following methods that are called by AngularJS at points in the life-cycle of the directive:
指令控制器可以提供以下由 AngularJS 在指令生命周期中的点调用的方法:
$onInit()
- Called on each controller after all the controllers on an element have been constructed and had their bindings initialized (and before the pre & post linking functions for the directives on this element).$onInit()
- 在元素上的所有控制器都被构造并初始化它们的绑定之后(以及在此元素上的指令的前后链接函数之前$onInit()
在每个控制器上调用。 This is a good place to put initialization code for your controller.这是放置控制器初始化代码的好地方。
For more information, see有关更多信息,请参阅
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.