Angularjs $stateChangeSuccess,始终与$state.params的值相同

Angularjs $stateChangeSuccess,始终与$state.params的值相同,angularjs,angular-ui-router,Angularjs,Angular Ui Router,我无法理解这一点,我真的在努力解决ui路由和$statechangesucture和$watch,有两个问题。我试图更改参数的值,当我更改状态时,请单击url。我认为知道我使用子状态很重要。在我当前的设置中,$stateChangeSuccess会在加载时触发两次,这意味着它会立即设置值。我想做的是只在url更改时设置它 我的路线是这样的,我试图设置一个参数的值 .state('medications', { url: '/medications',

我无法理解这一点,我真的在努力解决
ui路由
$statechangesucture
$watch
,有两个问题。我试图更改参数的值,当我更改
状态时,请单击url。我认为知道我使用子状态很重要。在我当前的设置中,
$stateChangeSuccess
会在加载时触发两次,这意味着它会立即设置值。我想做的是只在url更改时设置它

我的路线是这样的,我试图设置一个参数的值

.state('medications', {
                url: '/medications',
                templateUrl: '/partials/home.html',
                controller: 'mainController',
                params: { showSection: false },
                resolve: {
                    postPromise: ['medicationservice', function(medicationservice) {
                        return medicationservice.getAll();
                    }]
                }
            })
            .state('medications.add', {
                url: '/add',
                templateUrl: '/partials/home.html',
                controller: 'mainController',
                params: { showSection: true },
            })
在我的控制器中,我得到了以下信息,其中我在
init
上显式地将
openSesame
参数设置为
false
,但如上所述,它会触发并将其设置为
true

mainModule.controller('mainController', [
        '$scope',
        '$state',
        '$rootScope',
        'medicationservice',
        function($scope, $state, $rootScope, medicationservice) {
            $scope.medication = {};
            $scope.medications = medicationservice.posts;
            $scope.openSesame = false;

            $rootScope.$on("$stateChangeSuccess", function() {
                $scope.openSesame = true;
                console.log($scope.openSesame);
            });
}]);

状态更改只工作一次,也就是说,如果我使用$stateChangeSuccess的回调$rootScope

,您就可以访问state,fromState。或检查当前状态名称

$rootScope.$on("$stateChangeSuccess", function() {
          if ($state.current.name == "medications.add") {

             $scope.openSesame = true;
          } 

          console.log($scope.openSesame);
});

$stateChangeSuccess的回调,您可以从state访问state。或检查当前状态名称

$rootScope.$on("$stateChangeSuccess", function() {
          if ($state.current.name == "medications.add") {

             $scope.openSesame = true;
          } 

          console.log($scope.openSesame);
});

实际上,您可以使用
$statechangesucture
,而无需
$injection
$rootScope
。您可以改为在
$scope
对象上设置侦听器。请看以下内容

以及经修订的守则:

(function() {
  var app = angular.module('myApp', ['ui.router']);
  app.controller('mainController', [
    '$scope',
    '$state',
    function($scope, $state) {
      $scope.medication = {};
      $scope.openSesame = false;

      $scope.$on('$stateChangeSuccess',function(event, toState){
        $scope.openSesame = toState.params.showSection;
      });

      $scope.triggerMe = function() {
        alert('yes');
      };
    }
  ]);

  app.config([
    '$stateProvider',
    '$urlRouterProvider',
    function($stateProvider, $urlRouterProvider) {

      $stateProvider
        .state('medications', {
          url: '/medications',
          template: '<div>{{openSesame}}</div>',
          controller: 'mainController',
          params: {
            showSection: false
          }
        })
        .state('medications.add', {
          url: '/add',
          template: '<div>{{openSesame}}</div>',
          controller: 'mainController',
          params: {
            showSection: true
          },
        });

      $urlRouterProvider.otherwise('medications');
    }
  ]);
}());
(函数(){
var-app=angular.module('myApp',['ui.router']);
应用控制器(“主控制器”[
“$scope”,
“$state”,
函数($scope,$state){
$scope.medicing={};
$scope.openSesame=false;
$scope.$on(“$stateChangeSuccess”,函数(事件,toState){
$scope.openSesame=toState.params.showSection;
});
$scope.triggerMe=函数(){
警惕(“是”);
};
}
]);
app.config([
“$stateProvider”,
“$urlRouterProvider”,
函数($stateProvider,$urlRouterProvider){
$stateProvider
.州(“药物”{
url:“/药物”,
模板:{{openSesame}}},
控制器:“主控制器”,
参数:{
展示区:假
}
})
.state('drughts.add'{
url:“/add”,
模板:{{openSesame}}},
控制器:“主控制器”,
参数:{
展示区:正确
},
});
$urlRouterProvider。否则(“药物”);
}
]);
}());

单击按钮
medicing
medicing add
查看值的更改。

您可以实际使用
$statechangesucture
而无需
$injection
$rootScope
。您可以改为在
$scope
对象上设置侦听器。请看以下内容

以及经修订的守则:

(function() {
  var app = angular.module('myApp', ['ui.router']);
  app.controller('mainController', [
    '$scope',
    '$state',
    function($scope, $state) {
      $scope.medication = {};
      $scope.openSesame = false;

      $scope.$on('$stateChangeSuccess',function(event, toState){
        $scope.openSesame = toState.params.showSection;
      });

      $scope.triggerMe = function() {
        alert('yes');
      };
    }
  ]);

  app.config([
    '$stateProvider',
    '$urlRouterProvider',
    function($stateProvider, $urlRouterProvider) {

      $stateProvider
        .state('medications', {
          url: '/medications',
          template: '<div>{{openSesame}}</div>',
          controller: 'mainController',
          params: {
            showSection: false
          }
        })
        .state('medications.add', {
          url: '/add',
          template: '<div>{{openSesame}}</div>',
          controller: 'mainController',
          params: {
            showSection: true
          },
        });

      $urlRouterProvider.otherwise('medications');
    }
  ]);
}());
(函数(){
var-app=angular.module('myApp',['ui.router']);
应用控制器(“主控制器”[
“$scope”,
“$state”,
函数($scope,$state){
$scope.medicing={};
$scope.openSesame=false;
$scope.$on(“$stateChangeSuccess”,函数(事件,toState){
$scope.openSesame=toState.params.showSection;
});
$scope.triggerMe=函数(){
警惕(“是”);
};
}
]);
app.config([
“$stateProvider”,
“$urlRouterProvider”,
函数($stateProvider,$urlRouterProvider){
$stateProvider
.州(“药物”{
url:“/药物”,
模板:{{openSesame}}},
控制器:“主控制器”,
参数:{
展示区:假
}
})
.state('drughts.add'{
url:“/add”,
模板:{{openSesame}}},
控制器:“主控制器”,
参数:{
展示区:正确
},
});
$urlRouterProvider。否则(“药物”);
}
]);
}());


单击按钮
medicing
medicing add
查看值更改。

是否要更改状态更改的
showSection
参数?是,基本上,我想使用
showSection
参数设置
$scope.openseam
@inspiredok loot在我更新的答案中,你想在状态更改时更改
showSection
参数吗?是,基本上,我想使用
showSection
参数设置
$scope.openseam
@inspiredok loot在我更新的答案中,然后确定,这样就可以了。但这是一种正常的方法还是一种变通方法?我的意思是我必须在那里添加两条路线
=='medications'
=='medicings'。添加'
,在这种情况下,为我发送参数有什么意义?在其他情况下,我的意思是什么时候实际使用参数。1:正常方法是为子状态使用自己的控制器。这样你就不需要这个$stateChangeSuccessaccess手表了。第二:如果你不需要,就不要传递参数。OK,这样就行了。但这是一种正常的方法还是一种变通方法?我的意思是我必须在那里添加两条路线
=='medications'
=='medicings'。添加'
,在这种情况下,为我发送参数有什么意义?在其他情况下,我的意思是什么时候实际使用参数。1:正常方法是为子状态使用自己的控制器。这样你就不需要这个$stateChangeSuccessaccess手表了。第二:如果你不需要,就不要传递参数。我喜欢这个方法。我也喜欢上一个,但问题是,我一直收到一个错误,即当转到其他
状态时,无法设置
显示部分
,即使我没有使用任何
子状态
主控制器
。我来试试。代码是完全相同的还是有其他代码没有显示给我们?出了什么错?别担心,我的错,它起作用了。我只想测试一件事,如果可以的话,也许会问一个后续问题?我喜欢这种方法。我和你一样喜欢最后一个,但问题是