从默认路线

时间:2017-04-17 08:41:14

标签: angularjs

保持简短,它是一个单页面应用程序,所以index.php首先加载了默认模板URL和控制器。当我单击按钮并移动到新路径(新模板和控制器)时,该页面中没有任何内容,单击取消按钮不会弹出警报或将我重定向到主页面。

基本上 主页:列表销售页面(网址#1控制器#1) - >点击添加销售 - >添加销售页面(网址#2 controller2)>按钮不起作用

路由

var app = angular.module("phpApp", ["ngRoute"]);
app.config(['$routeProvider','$locationProvider',function ($routeProvider, $locationProvider) {
$routeProvider.when("/",{
    templateUrl:"view-list.php", controller: "listController"
})
    .when("/add", {
    templateUrl:"view-details.php", controller:"addController"
})
.when("/:index",{
    templateUrl:"view-details.php", controller:"editController"
})
.otherwise({
    redirectTo:"/"
});
}]);

默认templateURL (假设主索引页面只有1个按钮,即"添加销售")

<div class="form-group">
    <button data-ng-click="addSales()" class="btn btn-primary">Add Sales</button>
</div>

默认控制器

app.controller('listController', ['$scope', '$location','$routeParams', 'salesService', function($scope, $location, $routeParams, salesService){
$scope.data = salesService.getSales();

$scope.addSales = function(){
    $location.path('/add');
};

$scope.editSales = function(x)
{
    $location.path("/" + x);
};

$scope.delSales = function(x)
{
    if (confirm('Are you sure to delete?'))
    {
         alert("deleted item asdasd");
        //var index = $scope.bdays.indexOf(item);
        //$scope.bdays.splice(index, 1);
    }
    $location.path("/");
};


}]);

定向TemplateURL(添加销售页面) - 所以当我点击保存或取消时,它都不会将我重定向回主页

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

添加销售控制器

app.controller('addController', ['$scope', '$location', '$routeParams', 'salesService',
                              function($scope, $location, $routeParams, salesService){
$scope.save = function()
{
    salesService.addSales({id: $scope.Item.id, item: $scope.Item.item, name: $scope.Item.name, date: $scope.Item.date, price: $scope.Item.price});
    $location.path('/');
};

$scope.cancel = function()
{

    alert("deleted item asdasd");
    $scope.$apply(function() {
        $location.path('/');
    });
};
}]);

1 个答案:

答案 0 :(得分:-1)

尝试将html5mode上的$locationProvider属性设置为true