我试图运行一个函数,如果有人点击提交按钮或者在routeParams中有一个值(如果用户点击页面,param已经填写了。)我想要一个函数跑步。我有一个大脑放屁,似乎无法得到这个工作!
myApp.config([ '$routeProvider', function($routeProvider) {
$routeProvider.when('/:params?', {
templateUrl: 'myTemplate',
controller : 'myController'
}).otherwise({
redirectTo : '/'
});
} ]);
myApp.controller('ipCntrl', function($scope,$log,$http,$q,$routeParams, $location) {
$scope.runReport = function() {
$location.search({'ip': $routeParams['ip']})
}
});
myApp.controller('myController', function($scope,$log,$http,$q,$routeParams, $location) {
if ($routeParams['ip'])
{
$scope.ip = $routeParams['ip'];
runMyFunction();
}
<div ng-controller="ipCntrl">
<form ng-submit="runReport()">
<input class="form-control" ng-model="ip">
</form>
<div ng-view ></div>
</div>
<script type="text/ng-template" id="myTemplate">
HI!
</script>
现在的问题是什么?即使没有线路参数,我猜猜它的运行时间? – 2015-03-30 20:56:49
我在路由器中看不到任何'ip'参数 – 2015-03-30 20:56:52
在控制器的函数中使用$ rootScope。$ on('$ stateChangeStart', function(event,toState,toParams,fromState,fromParams){...})拦截状态转换。 https://github.com/angular-ui/ui-router/wiki#state-change-events – 2015-03-30 21:30:27