【问题标题】:$window.location.href is undefined with angularjs$window.location.href 未使用 angularjs 定义
【发布时间】:2015-07-31 08:36:46
【问题描述】:

我在 ASP.NET MVC6、angularjs 和 Bootstap 中编写了一个应用程序。 我想在引导模式关闭后重新加载页面。 为此,我使用 $window.location.href 但它未定义。

这是我在角度控制器中的方法:

 angular
    .module('LSapp')
    .controller('CustomersCtrl', CustomersCtrl);

CustomersCtrl.$inject = ['$scope', '$http', '$location', '$modal', '$templateCache', '$window'];

function CustomersCtrl($scope, $http, $location, $modal, $window) {
     $scope.edit = function(id)
    {
        var customer = getCustomer(id);
        console.log('Customer => FirstName : ' + customer.FirstName);
        var reqEditCustomer = $http({ url: '/api/customers/', dataType: 'json', method: 'PUT', data: JSON.stringify(customer), contentType: 'application/json; charset=utf-8' });
        reqEditCustomer.success(function (dataResult) {
            $scope.customer = dataResult;
            $scope.cancel();       
        });
        $scope.customers = getListCustomers();
        $window.location.href = '/';
    }
}

除重定向之外的所有运行。

我希望有人可以帮助我。欢迎任何帮助。

【问题讨论】:

  • 注入 $window 并使用 $window.location.reload();
  • 我试过了,但结果相同:reload() 无法读取未定义的属性“重新加载”。所以artm,我认为$window 是未定义的。

标签: angularjs twitter-bootstrap model-view-controller controller window


【解决方案1】:

你可以使用

$location.path('/');

而不是

$window.location.href = '/';

【讨论】:

  • yannick 我试过 $location.path('/')。我没有错误,但它不会使用新的编辑值重新加载我的列表视图。
【解决方案2】:

试试这个 -

$location.path('/').replace();
if(!$scope.$$phase) $scope.$apply()

【讨论】:

  • 我试过了,我没有错误,但它没有重新加载我的列表视图
【解决方案3】:

我试图从视图而不是模式重定向。是工作。 所以我认为这是我的模态导致问题的重定向。

这是我的完整控制器:

(function () {
'use strict';

angular
    .module('LSapp')
    .controller('CustomersCtrl', CustomersCtrl)
    .controller('CustomersGetCtrl', CustomersGetCtrl);

CustomersCtrl.$inject = ['$scope', '$http', '$location', '$modal', '$templateCache', '$window'];

function CustomersCtrl($scope, $http, $location, $modal, $window) {
    /*---------------------------------------------------------------------------------
     *                      Obtain Customer List
     *--------------------------------------------------------------------------------*/
    function getListCustomers()
    {
        var reqCustomers = $http.get('/api/Customers');
        reqCustomers.success(function (dataResult) {
            $scope.customers = dataResult;
        });

        return $scope.customers;
    }
    getListCustomers();
    /*---------------------------------------------------------------------------------
     *                      Obtain Customer by ID
     *--------------------------------------------------------------------------------*/
    function getCustomer(id) {
        var reqGetCustomer = $http({ url: '/api/customers/' + id, method: 'GET' });
        reqGetCustomer.success(function (dataResult) {
            $scope.customer = dataResult;
        })
        return $scope.customer;
    }

    $scope.edit = function(id)
    {
        var customer = getCustomer(id);
        console.log('Customer => FirstName : ' + customer.FirstName);
        var reqEditCustomer = $http({ url: '/api/customers/', dataType: 'json', method: 'PUT', data: JSON.stringify(customer), contentType: 'application/json; charset=utf-8' });
        reqEditCustomer.success(function (dataResult) {
            $scope.customer = dataResult;
            $scope.cancel();       
        });
        $scope.customers = getListCustomers();
        //This is that I tried to redirect
        //$window.location.href = '/';
        //$location.path('/').replace();
        //if(!$scope.$phase) $scope.$apply
    }

    /*---------------------------------------------------------------------------------
    *                      Manage Customer Details Modal
    *--------------------------------------------------------------------------------*/
    $scope.openDetails = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'Modals/Customers/details.html',
            controller: $scope.modalDetails,
            resolve: {
                id: function () {
                    return id
                }
            }
        });
    }

    $scope.modalDetails = function($scope, $modalInstance, id)
    {
        if (angular.isDefined(id)) {
            var reqGetCustomer = $http({ url: '/api/Customers/' + id, method: 'GET' });
            reqGetCustomer.success(function (dataResult) {
                $scope.customer = dataResult;
            });
        } else { alert('id is undefined'); }

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        }
    }

    /*---------------------------------------------------------------------------------
    *                      Manage Customer Edit Modal
    *--------------------------------------------------------------------------------*/

    $scope.openEdit = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'Modals/Customers/edit.html',
            controller: $scope.modalEdit,
            resolve: {
                id: function () {
                    return id
                }
            }
        });
    }

    $scope.modalEdit = function ($scope, $modalInstance, id) {
        if (angular.isDefined(id)) {
            var reqGetCustomer = $http({ url: '/api/Customers/' + id, method: 'GET' });
            reqGetCustomer.success(function (dataResult) {
                $scope.customer = dataResult;
            });
        } else { alert('id is undefined'); }

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        }
    }
}

//Controller to redirect since View
CustomersGetCtrl.$inject = ['$scope', '$http', '$routeParams', '$window'];

function CustomersGetCtrl($scope, $http, $routeParams, $window)
{
    function getCustomer()
    {
        var reqGetCustomer = $http({ url: '/api/customers/' + $routeParams.id, method: 'GET' })
        reqGetCustomer.success(function (dataResult) {
            $scope.customer = dataResult;
        })
    }
    getCustomer();

    $scope.edit = function () {
        $window.location.href = '/';
    }
}
})();

【讨论】:

    【解决方案4】:

    我通过使用 ui.router 而不是 ng -router 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-24
      • 2021-12-17
      • 2016-03-05
      • 1970-01-01
      • 2013-05-28
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多