我想添加某種解決方案/承諾,以防止用戶移動到某些頁面,直到他們創建了一個配置文件,添加了一個朋友等。有一個應用程序,一個控制器和一個服務。任何指導都會有所幫助!保持用戶不會去其他視圖ui-route
var app = angular.module('socialInternApp', ['ui.router']);
app.config(function($urlRouterProvider, $stateProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
//#1 Initial Page
.state('home', {
url: '/',
templateUrl: 'views/templates/home.html'
})
//#2 Landing Page(new users' profile page) - User is taken here when they submit the initial form on the Home page.
.state('landing', {
url: '/landing',
templateUrl: '/views/templates/landing.html'
})
//#3 Search for Friends - User can access this page only after they submit the initial form on the Home Page.
.state('friend-search', {
url: '/friend-search',
templateUrl: '/views/templates/friend-search.html',
controller: 'friendSearchCtrl'
})
//#4 Upadte Profile Information - User can access this as soon as they submit the initial form on the Home page.
.state('update', {
url: '/update',
templateUrl: 'views/templates/update.html',
})
//#5 Other users' profile page. User is taken here after clicking button on image overlay on the friend-search page.
.state('friend-profile', {
url: '/friend-profile/:id',
templateUrl: 'views/templates/friend-profile.html',
controller: 'friendProfileCtrl'
})
//#6 New users' friends list. Only can access after they have added a friend.
.state('friends', {
url: '/friends',
templateUrl: '/views/templates/friends.html'
});
});
控制器代碼:
app.controller('mainCtrl', function($scope, $state, mainSvrc){
$scope.$state = $state;
$scope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
// Check whether user input form is submitted
if(!formSubmitted){
event.preventDefault();
}
});
//Local storage for new user profile and updated profile.//
$scope.currentUser = JSON.parse(localStorage.getItem('profile'));
var formSubmitted = false;
$scope.createUser = function(obj, $event) {
$scope.currentUser = mainSvrc.createUser(obj);
formSubmitted = true;
$state.go('landing');
swal(
'',
'Your Information has been saved!',
'success'
);
};
});
服務代碼:
app.service('mainSvrc', function() {
//Stores users' data in local storage after clicking the save button on either form.
this.createUser = function(user) {
localStorage.setItem('profile', JSON.stringify(user));
return user;
};
HTML代碼:
<form class="form-home">
<input type="text"
ng-model="newUser.name"
placeholder="Full Name"
onfocus="this.placeholder = ''"
onblur="this.placeholder = 'Full Name'" />
<input type="text"
ng-model="newUser.tagline"
placeholder="Tagline"
onfocus="this.placeholder = ''"
onblur="this.placeholder = 'Tagline'">
<input type="text"
ng-model="newUser.profileUrl"
placeholder="Profile Image Url"
onfocus="this.placeholder = ''"
onblur="this.placeholder = 'Profile Image Url'">
<textarea rows="15" cols="50"
ng-model="newUser.bio"
placeholder="Bio"
onfocus="this.placeholder = ''"
onblur="this.placeholder = 'Bio'">
</textarea>
<button type="submit" ng-click="createUser(newUser, event)"
>
Save Changes
</button>
</form>
你能告訴你的控制器和視圖代碼? –
@RaviTeja補充說。 – StuffedPoblano
您可以使用$ locationChangeStart來觸發URL更改事件並阻止用戶進行導航 – Nitheesh