angular 跳转返回上一页
<div class="cnblogs_Highlighter"><pre class="brush:javascript;gutter:true;">HTML中<br><script src="lib/angular/angular-1.4.9/angular.js"></script>
<script src="lib/angular/angular-ui-router.min.js"></script>
</pre>
</div>
<p> app中</p>
<div class="cnblogs_code">
<pre> angular.module('ConsoleUIApp', ['ui.router','ui.bootstrap'<span style="color: rgba(0, 0, 0, 1)">])
.config(</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> ($stateProvider, $urlRouterProvider, $httpProvider) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> For any unmatched url, redirect to /state1</span>
$urlRouterProvider.otherwise("/home"<span style="color: rgba(0, 0, 0, 1)">);
$stateProvider
.state(</span>'home'<span style="color: rgba(0, 0, 0, 1)">, {
url: </span>"/home"<span style="color: rgba(0, 0, 0, 1)">,
templateUrl: </span>"views/home.html"<span style="color: rgba(0, 0, 0, 1)">,
controller: </span>'HomeCtrl'<span style="color: rgba(0, 0, 0, 1)">
})
.state(</span>'testing'<span style="color: rgba(0, 0, 0, 1)">, {
url: </span>"/testing"<span style="color: rgba(0, 0, 0, 1)">,
templateUrl: </span>"views/testing.html"<span style="color: rgba(0, 0, 0, 1)">,
controller: </span>'TestingCtrl'<span style="color: rgba(0, 0, 0, 1)">
})
})
.run(</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">($rootScope, growl, $state, $stateParams) {
$rootScope.$state </span>=<span style="color: rgba(0, 0, 0, 1)"> $state;
$rootScope.$stateParams </span>=<span style="color: rgba(0, 0, 0, 1)"> $stateParams;
$rootScope.$on(</span>"$stateChangeSuccess",<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(event, toState, toParams, fromState, fromParams) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> to be used for back button //won't work when page is reloaded.</span>
$rootScope.previousState_name =<span style="color: rgba(0, 0, 0, 1)"> fromState.name;
$rootScope.previousState_params </span>=<span style="color: rgba(0, 0, 0, 1)"> fromParams;
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">back button function called from back button's ng-click="back()"</span>
$rootScope.back = <span style="color: rgba(0, 0, 255, 1)">function</span>() {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">实现返回的函数</span>
<span style="color: rgba(0, 0, 0, 1)"> $state.go($rootScope.previousState_name,$rootScope.previousState_params);
};
});</span></pre>
</div>
<p>controller:</p>
<div class="cnblogs_code">
<pre> $scope.sub = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(addRode) {
$rootScope.back()</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">直接使用</span>
} </pre>
</div>
<p>第二种方法:</p>
<p>window.history.back() </p><br><br>
来源:https://www.cnblogs.com/Angiy/p/11655908.html
頁:
[1]