似乎Angular在路线改变时重新渲染整个视图。请参阅此示例:http://jsfiddle.net/RSHG8/1/
HTML:
<div class="nav">
<a href="#/one">One</a><a href="#/two">Two</a>
</div>
<div ng-app="app" id="ng-app">
<div ng-view=ng-view></div>
</div>
var app = angular.module('app', []);
JS:
app.config(function ($routeProvider) {
$routeProvider.when('/one', {
template: 'Template {{template}}<br>Enter some text: <input type="text" /> then click "Two"',
controller: 'one'
})
.when('/two', {
template: 'Template {{template}}<br>Click back to view "One" to see changes undone',
controller: 'two'
})
.otherwise({
redirectTo: '/one'
});
});
app.controller("one", function ($scope) {
$scope.template = "One";
});
app.controller("two", function ($scope) {
$scope.template = "Two";
});
在视图之间切换时,忘记了用户输入的文本。对于非角度的东西,这也是一个问题。例如。一个jQuery expand-collapse插件。如果用户展开一定数量的元素,离开视图并返回,则状态将重置为折叠的所有内容。
是否可以将Angular设置为简单的显示/隐藏视图,而不是在路线更改时擦除重新渲染?
答案 0 :(得分:0)
您可以尝试将ng-model分配给输入标记,并将值存储在父控制器中。
HTML
<div class="nav">
<a href="#/one">One</a><a href="#/two">Two</a>
</div>
<div ng-app="app" id="ng-app">
<div controller="AppCtrl">
<div ng-view=ng-view></div>
</div>
</div>
var app = angular.module('app', []);
JS
app.config(function ($routeProvider) {
$routeProvider.when('/one', {
template: 'Template {{template}}<br>Enter some text: <input type="text" ng-model="$parent.inputValue" /> then click "Two"',
controller: 'one'
})
.when('/two', {
template: 'Template {{template}}<br>Click back to view "One" to see changes undone',
controller: 'two'
})
.otherwise({
redirectTo: '/one'
});
});
app.controller("AppCtrl", function AppCtrl($scope) {
$scope.inputValue = "";
});
app.controller("one", function ($scope) {
$scope.template = "One";
});
app.controller("two", function ($scope) {
$scope.template = "Two";
});