Javascript 在angularjs中调用控制器时如何加载函数
我有一个函数Javascript 在angularjs中调用控制器时如何加载函数,javascript,angularjs,controller,mean-stack,angularjs-routing,Javascript,Angularjs,Controller,Mean Stack,Angularjs Routing,我有一个函数 state('showList', { url: '/listView', templateUrl: 'app/templates/showlist.html', controller: "showListCtrl" }). 我的控制器 .controller('overViewCtrl', function ($scope) { $scope.getList = fu
state('showList', {
url: '/listView',
templateUrl: 'app/templates/showlist.html',
controller: "showListCtrl"
}).
我的控制器
.controller('overViewCtrl', function ($scope) {
$scope.getList = function(){
//A http call
}
});
当我的路由更改为“/listView”时,会调用overView控制器,但不会调用函数getList(),但我在刷新页面时会被调用。请任何人帮助我。我只想在加载控制器时调用该函数。谢谢
当我的路由更改为“/listview”时您可以在函数外部声明代码
.controller('overViewCtrl', function ($scope)
//put it here without a function
});
或者您可以在页面上使用ng init调用函数您可以在函数内部声明代码
.controller('overViewCtrl', function ($scope)
//put it here without a function
});
.controller('overViewCtrl', function ($scope) {
$scope.getList = function(){
//A http call
}
$scope.getList() //just call it here
});
或者您可以在页面上使用ng init调用函数
.controller('overViewCtrl', function ($scope) {
$scope.getList = function(){
//A http call
}
$scope.getList() //just call it here
});
或者我建议您使用resolve
最初获取数据
或者我建议您使用resolve
最初获取数据
加载控制器后,您可以使用ui路由器的功能使控制器更轻薄,但仍然可以访问解析的数据
// router
$stateProvider.state('myState', {
resolve: {
simpleObj: function(){
return {value: 'simple!'};
}
}
}
// controller
['simpleObj', function(simpleObj) {
console.log(simpleObj); // ==> value: 'simple!'
}]
您可以使用ui路由器的功能使控制器更为精简,但在控制器加载后仍可以访问已解析的数据
// router
$stateProvider.state('myState', {
resolve: {
simpleObj: function(){
return {value: 'simple!'};
}
}
}
// controller
['simpleObj', function(simpleObj) {
console.log(simpleObj); // ==> value: 'simple!'
}]
如果我放在那里,有一个错误或者getlist不是函数。你不需要有getlist如果我放在那里,有一个错误或者getlist不是函数。你不需要有getlist