forked from modernduck/ng1-day2-router-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (50 loc) · 1.9 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
angular.module('myApp', ['ngRoute', 'myFactory'])
.config( function($routeProvider){
//going to config which route to go where
$routeProvider.when("/", {
templateUrl:"pages/main.html",
controller:"MainController"
})
.when('/create', {
templateUrl:"pages/create.html",
controller:"UserController"
})
.when("/user/:id", {
templateUrl:"pages/invoice.html",
controller:"UserController"
})
})
.controller("MainController", ['$scope', 'userProvider', function($scope, userProvider){
//recommended format
userProvider.load();
$scope.users = userProvider.getUsers();
}])
.controller("UserController", function($scope, $routeParams, userProvider, $location){
userProvider.load();
//$scope.user = {id:1, name:"sompop", lastname:"kulapalanont", salary:50000, picture:"http://placehold.it/100x100"};
if($routeParams.id){
//update mode
$scope.user = userProvider.getUserById($routeParams.id);
//happen when click sve button
$scope.save = function(){
userProvider.updateUserById($routeParams.id, $scope.user.name, $scope.user.lastname, $scope.user.salary, $scope.user.picture)
userProvider.save();
$location.path('/');
}
}else{
$scope.user = {};
$scope.save = function(){
userProvider.createUser($scope.user.name, $scope.user.lastname, $scope.user.salary, $scope.user.picture)
userProvider.save();
$location.path('/');
}
}
}).directive('userForm', function(){
return {
scope:{
user:"=",
save:"=onSave"
},
templateUrl : "widgets/user-form.html"
}
})