注意:相同的 viewDirective.js但是 elswhere 可以用来证明这个事实——如果我们离开状态,$scope 被销毁: function cleanupLastView() {如果(上一个El){previousEl.remove();previousEl = null;}如果(当前范围){currentScope.$destroy();当前范围 = 空;}...扩展我创建了这里的一个工作示例,具有以下两种状态:.controller('ParentCtrl', ['$scope', function ($scope) {$scope.Model = {SharedName: "这是共享名称",}$scope.NotSharedName = $scope.NotSharedName||这个名字是被克隆的,但又以自己的方式生活";}]).controller('ChildCtrl', ['$scope', function ($scope) {}])以及这两种方式如何改变值(都将遵循上述逻辑):这将在所有孩子和父母之间共享<input ng-model="Model.SharedName"/></p><p>这将始终从父级复制,然后在每个子级中存在<input ng-model="NotSharedName"/></p>在这里I use UI-Router routing. When I change path from a state to another and going back to same state, I see that old $scope in state is there (with it's properties).I want to destroy that $scope before state changes, So when I come back to state for second time, there will be a clean new scope. I tried to access scope in this event:$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { // fromState.$scope.$destroy();});But there isn't any reference to $scope. Can I access scope before change state in angular UI-Router? 解决方案 I would say, that what you experience is a bit different than you described, or you thought what is happening. Please, check for example this:How do I share $scope data between states in angularjs ui-router?scope and controller instantiation with ui routerIn general, once the state change is done (not rejected), the old $scope is for sure destroyed. If we navigate then back, new $scope is created for us. But this $scope is created this way:The source code of theviewDirective.js function updateView(firstTime) { var newScope, name = getUiViewName(scope, attrs, $element, $interpolate), previousLocals = name && $state.$current && $state.$current.locals[name]; if (!firstTime && previousLocals === latestLocals) return; // nothing to do // HERE newScope = scope.$new(); ...The construct: scope.$new(); is a key to understanding. This in fact means, that we use prototypical inheritanceWhat are the nuances of scope prototypal / prototypical inheritance in AngularJS?And that in a nutshell could be described: So if parent contains some reference (has '.' in the path) like this// parent scope$scope.Model = { ...};And any child state will change that like this$scope.Model.name = "User";That value will be stored in parent state $scope and available again ... for any next child of this state.NOTE: the same viewDirective.js but elswhere could be used to demonstrate the fact - $scope is destroyed if we leave the state: function cleanupLastView() { if (previousEl) { previousEl.remove(); previousEl = null; } if (currentScope) { currentScope.$destroy(); currentScope = null; } ...EXTENDI created a working example here, with these two states:.controller('ParentCtrl', ['$scope', function ($scope) { $scope.Model = { SharedName: "This is shared name", } $scope.NotSharedName = $scope.NotSharedName || "This name is cloned, but then lives its own way";}]).controller('ChildCtrl', ['$scope', function ($scope) {}])And these two ways how to change values (all will follow the logic described above):<p>this will be shared among all children and parent <input ng-model="Model.SharedName" /></p><p>this will always copied from parent, live then in each child <input ng-model="NotSharedName" /></p>Check it here 这篇关于在 ui-router 中更改路径之前销毁范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 15:01