<small id='8659W'></small><noframes id='8659W'>

      • <bdo id='8659W'></bdo><ul id='8659W'></ul>
      <tfoot id='8659W'></tfoot>

      <legend id='8659W'><style id='8659W'><dir id='8659W'><q id='8659W'></q></dir></style></legend>

    1. <i id='8659W'><tr id='8659W'><dt id='8659W'><q id='8659W'><span id='8659W'><b id='8659W'><form id='8659W'><ins id='8659W'></ins><ul id='8659W'></ul><sub id='8659W'></sub></form><legend id='8659W'></legend><bdo id='8659W'><pre id='8659W'><center id='8659W'></center></pre></bdo></b><th id='8659W'></th></span></q></dt></tr></i><div id='8659W'><tfoot id='8659W'></tfoot><dl id='8659W'><fieldset id='8659W'></fieldset></dl></div>

      1. AngularJS ui-router $state.go('^') 仅更改地址栏中的 URL,但不加载

        时间:2023-10-21
          <i id='PTqO8'><tr id='PTqO8'><dt id='PTqO8'><q id='PTqO8'><span id='PTqO8'><b id='PTqO8'><form id='PTqO8'><ins id='PTqO8'></ins><ul id='PTqO8'></ul><sub id='PTqO8'></sub></form><legend id='PTqO8'></legend><bdo id='PTqO8'><pre id='PTqO8'><center id='PTqO8'></center></pre></bdo></b><th id='PTqO8'></th></span></q></dt></tr></i><div id='PTqO8'><tfoot id='PTqO8'></tfoot><dl id='PTqO8'><fieldset id='PTqO8'></fieldset></dl></div>
            <bdo id='PTqO8'></bdo><ul id='PTqO8'></ul>
              <tbody id='PTqO8'></tbody>
            • <small id='PTqO8'></small><noframes id='PTqO8'>

                <legend id='PTqO8'><style id='PTqO8'><dir id='PTqO8'><q id='PTqO8'></q></dir></style></legend>

              1. <tfoot id='PTqO8'></tfoot>

                  本文介绍了AngularJS ui-router $state.go('^') 仅更改地址栏中的 URL,但不加载控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 angularjs ui-router 创建一个Todo 应用程序".它有 2 列:

                  I am trying to create a "Todo App" with angularjs ui-router. It has 2 columns:

                  • 第 1 列:待办事项列表
                  • 第 2 列:Todo 详细信息或 Todo 编辑表单

                  在保存待办事项后的编辑和创建控制器中,我想重新加载列表以显示适当的更改.问题:在创建或更新Todo时调用$state.go('^')后,浏览器中的URL变回/api/todo,但是ListCtrl 未执行,即 $scope.search 未调用,因此未检索到 Todo 列表(包含更改的项目),第 2 列中也不显示第一个 Todo 的详细信息(而是,它变成空白).

                  In the Edit and Create controller after saving the Todo I would like to reload the list to show the appropriate changes. The problem: after calling $state.go('^') when the Todo is created or updated, the URL in the browser changes back to /api/todo, but the ListCtrl is not executed, i.e. $scope.search is not called, hence the Todo list (with the changed items) is not retrieved, nor are the details of the first Todo displayed in Column 2 (instead, it goes blank).

                  我什至尝试过 $state.go('^', $stateParams, { reload: true, inherit: false, notify: false });,没有运气.

                  I have even tried $state.go('^', $stateParams, { reload: true, inherit: false, notify: false });, no luck.

                  如何进行状态转换,以便最终执行控制器?

                  来源:

                  var TodoApp = angular.module('TodoApp', ['ngResource', 'ui.router'])
                      .config(function ($stateProvider, $urlRouterProvider) {
                          $urlRouterProvider.otherwise('/api/todo');
                  
                          $stateProvider
                              .state('todo', {
                                  url: '/api/todo',
                                  controller: 'ListCtrl',
                                  templateUrl: '/_todo_list.html'
                              })
                              .state('todo.details', {
                                  url: '/{id:[0-9]*}',
                                  views: {
                                      'detailsColumn': {
                                          controller: 'DetailsCtrl',
                                          templateUrl: '/_todo_details.html'
                                      }
                                  }
                              })
                              .state('todo.edit', {
                                  url: '/edit/:id',
                                  views: {
                                      'detailsColumn': {
                                          controller: 'EditCtrl',
                                          templateUrl: '/_todo_edit.html'
                                      }
                                  }
                              })
                              .state('todo.new', {
                                  url: '/new',
                                  views: {
                                      'detailsColumn': {
                                          controller: 'CreateCtrl',
                                          templateUrl: '/_todo_edit.html'
                                      }
                                  }
                              })
                          ;
                  
                      })
                  ;
                  
                  TodoApp.factory('Todos', function ($resource) {
                      return $resource('/api/todo/:id', { id: '@id' }, { update: { method: 'PUT' } });
                  });
                  
                  var ListCtrl = function ($scope, $state, Todos) {
                      $scope.todos = [];
                  
                      $scope.search = function () {
                          Todos.query(function (data) {
                              $scope.todos = $scope.todos.concat(data);
                              $state.go('todo.details', { id: $scope.todos[0].Id });
                          });
                      };
                  
                      $scope.search();
                  };
                  
                  var DetailsCtrl = function ($scope, $stateParams, Todos) {
                      $scope.todo = Todos.get({ id: $stateParams.id });
                  };
                  
                  var EditCtrl = function ($scope, $stateParams, $state, Todos) {
                      $scope.action = 'Edit';
                  
                      var id = $stateParams.id;
                      $scope.todo = Todos.get({ id: id });
                  
                      $scope.save = function () {
                          Todos.update({ id: id }, $scope.todo, function () {
                              $state.go('^', $stateParams, { reload: true, inherit: false, notify: false });
                          });
                      };
                  };
                  
                  var CreateCtrl = function ($scope, $stateParams, $state, Todos) {
                      $scope.action = 'Create';
                  
                      $scope.save = function () {
                          Todos.save($scope.todo, function () {
                              $state.go('^');
                          });
                      };
                  };
                  

                  推荐答案

                  非常感谢 Radim Khler 指出 $scope 是继承的.通过 2 个小改动,我设法解决了这个问题.请参阅下面的代码,我评论了我添加额外行的位置.现在它就像一个魅力.

                  Huge thanks for Radim Khler for pointing out that $scope is inherited. With 2 small changes I managed to solve this. See below code, I commented where I added the extra lines. Now it works like a charm.

                  var TodoApp = angular.module('TodoApp', ['ngResource', 'ui.router'])
                      .config(function ($stateProvider, $urlRouterProvider) {
                          $urlRouterProvider.otherwise('/api/todo');
                  
                          $stateProvider
                              .state('todo', {
                                  url: '/api/todo',
                                  controller: 'ListCtrl',
                                  templateUrl: '/_todo_list.html'
                              })
                              .state('todo.details', {
                                  url: '/{id:[0-9]*}',
                                  views: {
                                      'detailsColumn': {
                                          controller: 'DetailsCtrl',
                                          templateUrl: '/_todo_details.html'
                                      }
                                  }
                              })
                              .state('todo.edit', {
                                  url: '/edit/:id',
                                  views: {
                                      'detailsColumn': {
                                          controller: 'EditCtrl',
                                          templateUrl: '/_todo_edit.html'
                                      }
                                  }
                              })
                              .state('todo.new', {
                                  url: '/new',
                                  views: {
                                      'detailsColumn': {
                                          controller: 'CreateCtrl',
                                          templateUrl: '/_todo_edit.html'
                                      }
                                  }
                              })
                          ;
                  
                      })
                  ;
                  
                  TodoApp.factory('Todos', function ($resource) {
                      return $resource('/api/todo/:id', { id: '@id' }, { update: { method: 'PUT' } });
                  });
                  
                  var ListCtrl = function ($scope, $state, Todos) {
                      $scope.todos = [];
                  
                      $scope.search = function () {
                          Todos.query(function (data) {
                              $scope.todos = $scope.todos(data); // No concat, just overwrite
                              if (0 < $scope.todos.length) { // Added this as well to avoid overindexing if no Todo is present
                                  $state.go('todo.details', { id: $scope.todos[0].Id });
                              }
                          });
                      };
                  
                      $scope.search();
                  };
                  
                  var DetailsCtrl = function ($scope, $stateParams, Todos) {
                      $scope.todo = Todos.get({ id: $stateParams.id });
                  };
                  
                  var EditCtrl = function ($scope, $stateParams, $state, Todos) {
                      $scope.action = 'Edit';
                  
                      var id = $stateParams.id;
                      $scope.todo = Todos.get({ id: id });
                  
                      $scope.save = function () {
                          Todos.update({ id: id }, $scope.todo, function () {
                              $scope.search(); // Added this line
                              //$state.go('^'); // As $scope.search() changes the state, this is not even needed.
                          });
                      };
                  };
                  
                  var CreateCtrl = function ($scope, $stateParams, $state, Todos) {
                      $scope.action = 'Create';
                  
                      $scope.save = function () {
                          Todos.save($scope.todo, function () {
                              $scope.search(); // Added this line
                              //$state.go('^'); // As $scope.search() changes the state, this is not even needed.
                          });
                      };
                  };
                  

                  这篇关于AngularJS ui-router $state.go('^') 仅更改地址栏中的 URL,但不加载控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:带有 ui-router 的 angular-material 中每个选项卡的单独控制器 下一篇:AngularJS ui-router:如何全局解析所有路由的典型数据?

                  相关文章

                  <i id='pYsCf'><tr id='pYsCf'><dt id='pYsCf'><q id='pYsCf'><span id='pYsCf'><b id='pYsCf'><form id='pYsCf'><ins id='pYsCf'></ins><ul id='pYsCf'></ul><sub id='pYsCf'></sub></form><legend id='pYsCf'></legend><bdo id='pYsCf'><pre id='pYsCf'><center id='pYsCf'></center></pre></bdo></b><th id='pYsCf'></th></span></q></dt></tr></i><div id='pYsCf'><tfoot id='pYsCf'></tfoot><dl id='pYsCf'><fieldset id='pYsCf'></fieldset></dl></div>

                  <small id='pYsCf'></small><noframes id='pYsCf'>

                      <bdo id='pYsCf'></bdo><ul id='pYsCf'></ul>

                  1. <tfoot id='pYsCf'></tfoot>

                      <legend id='pYsCf'><style id='pYsCf'><dir id='pYsCf'><q id='pYsCf'></q></dir></style></legend>