2017-02-08 3 views
1

После добавления музыки я хочу, чтобы перенаправить на главной странице на отменяют и сохранить ..Перенаправление на страницу по нажатию кнопки в угловом JS

var app = angular.module("musicApp", ["ngRoute"]); 
app.config(function($routeProvider) { 
    $routeProvider.when("/Items", { 
     templateUrl: "view-list.html", 
     controller: "listController" 
    }) 
    .when("/Items/add", { 
     templateUrl: "view-detail.html", 
     controller: "addController" 
    }) 
    .when("/Items/:index", { 
     templateUrl: "view-detail.html", 
     controller: "editController" 
    }) 
    .otherwise({ 
     redirectTo: "/Items" 
    }); 
}); 


app.factory("productService", ["$rootScope", function($rootScope){ 
    var service={}; 
    var data = [{ 
     name: "Artist1", 
     genre: "Genre1", 
     rating: "Rating1" 
    }, { 
     name: "Artist2", 
     genre: "Genre2", 
     rating: "Rating2" 
    }]; 

    service.getProducts = function(){}; 
    service.addProduct = function(product){}; 
    service.editProduct = function(product){}; 
    return service; 
}]); 
app.controller("listController", ["$scope", "$location", "$routeParams", 
    function($scope, $location, $routeParams) { 

    $scope 

    $scope.addProduct = function() { 
     $location.path("/Items/add"); 
    }; 

    $scope.editItem = function(index) { 
     $location.path("/Items/" + index); 
    }; 

    } 
]); 

app.controller("addController", ["$scope", "$location", "$routeParams", 
    function($scope, $location, $routeParams) { 
    $scope.save = function() { 
     $location.url("/Items"); 
    }; 
    $scope.cancel = function() { 
     $location.path("/Items"); 
    }; 
    } 
]); 


app.controller("editController", ["$scope", "$location", "$routeParams", 
    function($scope, $location, $routeParams) { 
    $scope.save = function() { 
     $location.path("/Items"); 
    }; 
    $scope.cancel = function() { 
     $location.path("/Items"); 
    }; 
    } 
]); 

Основной часть, чтобы перейти на главной странице, как:

app.controller("addController", ["$scope", "$location", "$routeParams", 
    function($scope, $location, $routeParams) { 
    $scope.save = function() { 
     $location.url("/Items"); 
    }; 
    $scope.cancel = function() { 
     $location.path("/Items"); 
    }; 
    } 
]); 

Это не перенаправляет меня на главную страницу, где все мои продукты перечислены ..

+0

у вас есть скрипку для вашего приложения или ссылки? – Khaleel

+1

yes @Khaleel https://plnkr.co/edit/tMAUSbCR4sXga9ilnmMe?p=preview, пожалуйста, проверьте это – Dhara

+0

, у вас есть ошибка в вашем «view-detail.html». Вы забыли поместить двойные кавычки в 'data-ng-model =" item.name "и другую модель – pryxen

ответ

2

вид-detail.html

<div class="form-group"> 
    <label for="txtName">Product Name</label> 
    <input type="text" id="txtName" class="form-control" data-ng-model="item.name" /> 
</div> 

<div class="form-group"> 
    <label for="cboGenre">Genre</label> 
    <input type="text" id="cboGenre" class="form-control" data-ng-model="item.genre"/> 
</div> 

<div class="form-group"> 
    <label for="cboRating">Rating</label> 
    <input type="text" id="cboRating" class="form-control" data-ng-model="item.rating"/> 
</div> 

<div class="form-group"> 
    <button class="btn bth-primary" data-ng-click="save()">Save</button> 
    <button data-ng-click="cancel()" class="btn bth-primary">Cancel</button> 
</div> 

Вы забыли поставить " " двойные кавычки в вашем data-ng-model=item.name должно быть data-ng-model="item.name"

Смежные вопросы