- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个页面,其中包含分布在多个选项卡上的 HTML 表单元素。我希望用户能够切换选项卡,而不会丢失他在表单元素上输入的数据(并且我还想保存不必要的数据重新加载)。我仍然希望能够向某人传递特定选项卡的链接。
听起来 ui-extras 粘性状态应该完全满足我的需要。只是我一直没有成功地让它发挥作用。
我研究了example源代码,当我启动示例时,我例如在 Line 57 in controllers.js 中找到的库存 Controller 的构造函数中使用 firebug 设置断点,我看到构造函数只被触发一次。然而,我的 Controller 构造函数被一遍又一遍地触发,并且我的应用程序的行为非常类似于粘性状态不存在,尽管如果我启用粘性状态调试,它会告诉我它正在做某事(停用和重新激活状态)。
我发现有人在 this answer 的评论中指出粘性状态仅适用于命名 View 。 ,所以我尝试给我的 View 命名,但这没有任何区别。
我尝试在选项卡之前插入明确的“根状态”。
我尝试通过 ng-controller 或状态中的 Controller 定义插入 Controller 。
github 上的示例是一个很好的展示,但远远超出了最小限度,并且很难看出实际需要什么和不需要什么。
开始使用粘性状态所需的最小示例是什么?(奖励:我的代码出了什么问题?)。
此处供引用的是 plunkr with my failed attempt (查看历史记录以查看之前尝试的选择)。
这是我当前失败的源代码:
var log = '';
function mkController(msg) {
return function($scope) {
// This is the constructor of a controller
// I'd expect this constructor to the first time a state is loaded.
// When switching to a sister state and back it should not be called again.
if (!$scope.random) {
// I expect the $scope object to be retained when changing states for and
// back. So even if my assumption that the controller will be persistent
// would be wrong this is to check whether the $scope survives.
// If the scope survives the random number will be initialized only once
// and then it won't change anymore:
$scope.random = Math.round(Math.random()*10000);
}
// This log will tell us how often the controller constructor has been called
// (Should be only once, I think)
log += 'creating: ' + msg + '\n';
this.message = log;
}
}
angular.module('plunker', ['ui.router', 'ct.ui.router.extras.sticky', 'ct.ui.router.extras.dsr'])
.controller('ControllerA', mkController('ControllerA'))
.controller('ControllerB', mkController('ControllerB'))
.run(function($templateCache) {
$templateCache.put('root.html', '<div ui-view="myview"></div>');
$templateCache.put('templateA.html', '<div ng-controller="ControllerA as controller"><pre>Random: {{random}}, Message (templateA): {{controller.message}}</pre></div>');
$templateCache.put('templateB.html', '<div ng-controller="ControllerB as controller"><pre>Random: {{random}}, Message (templateB): {{controller.message}}</pre></div>');
})
.config(function($stateProvider) {
$stateProvider
.state('root', {
url: '/',
templateUrl: 'root.html'
}).state('root.stateA', {
url: '/stateA',
views: {
myview: {
templateUrl: 'templateA.html',
}
},
sticky: true,
deepStateRedirect: true
}).state('root.stateB', {
url: '/stateB',
views: {
myview: {
templateUrl: 'templateB.html',
}
},
sticky: true,
deepStateRedirect: true
});
})
.config(function($stickyStateProvider) {
$stickyStateProvider.enableDebug(true);
});
最佳答案
我明白了:
演示页面中提到了两者,但我必须承认,我并没有真正仔细阅读演示页面的文本,因为我认为这只是有关此特定演示的信息。
无论如何:这是一个我认为是保持粘性状态所需的最低限度的示例:
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link href="style.css" rel="stylesheet" />
<script data-semver="1.3.12" src="https://code.angularjs.org/1.3.12/angular.js" data-require="angular.js@1.3.x"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.13/angular-ui-router.js"></script>
<script src="ui-router-extras.js"></script>
<script src="app.js"></script>
</head>
<body>
<ul class="nav nav-tabs nav-tabs-head" role="tablist">
<li ui-sref-active="active"><a ui-sref="root.stateA" role="tab">StateA</a></li>
<li ui-sref-active="active"><a ui-sref="root.stateB" role="tab">StateB</a></li>
</ul>
<div ui-view="" />
</body>
</html>
var log = '';
function mkController(msg) {
return function($scope) {
// This is the constructor of a controller
// I'd expect this constructor to the first time a state is loaded.
// When switching to a sister state and back it should not be called again.
if (!$scope.random) {
// I expect the $scope object to be retained when changing states for and
// back. So even if my assumption that the controller will be persistent
// would be wrong this is to check whether the $scope survives.
// If the scope survives the random number will be initialized only once
// and then it won't change anymore:
$scope.random = Math.round(Math.random()*10000);
}
// This log will tell us how often the controller constructor has been called
// (Should be only once, I think)
log += 'creating: ' + msg + '\n';
this.message = 'Current msg: ' + msg + '\n\n' + log;
}
}
angular.module('plunker', ['ui.router', 'ct.ui.router.extras.sticky'])
.controller('ControllerA', mkController('ControllerA'))
.controller('ControllerB', mkController('ControllerB'))
.config(function($stateProvider) {
$stateProvider
.state('root', {
url: '/',
template: '<div ui-view="a" ng-show="$state.includes(\'root.stateA\')"></div><div ui-view="b" ng-show="$state.includes(\'root.stateB\')"></div>'
}).state('root.stateA', {
url: '/stateA',
views: {
'a@root': {
template: '<div ng-controller="ControllerA as controller"><h1>A</h1><pre>Random: {{random}}, Message (templateA): {{controller.message}}</pre></div>',
}
},
sticky: true
}).state('root.stateB', {
url: '/stateB',
views: {
'b@root': {
template: '<div ng-controller="ControllerB as controller"><h1>B</h1><pre>Random: {{random}}, Message (templateB): {{controller.message}}</pre></div>',
}
},
sticky: true
});
})
.config(function($stickyStateProvider) {
$stickyStateProvider.enableDebug(true);
})
.run(function ($rootScope, $state) {
$rootScope.$state = $state;
});
关于angularjs - 保留 angularjs ui-router 状态与 ui-router-extras 中的粘性状态所需的最低限度是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28333891/
我有一个已发布的库,其中包含一个在其模板中使用 [routerLink] 的组件。在我的应用程序中安装库后,我收到错误 NullInjectorError:R3InjectorError(AppMod
/~/@ReMix-Run/Router/dist/router.cjs.js模块分析失败:/HOME/sharib/Desktop/Full Stack Developer Tutorial/MER
在 vuejs 2 中,我能够通过返回渲染 html 标签直接在路由器中渲染。我正在尝试使用 vue-router 4 在 vue3 中做同样的事情,但它似乎不起作用: { path:
在我的应用程序中,我使用 vue-router。当我使用 router.map({}) 将路由传递给路由器时,路由可以工作,但当我在构造函数中传递它们时,路由就不起作用。知道这是为什么吗? // wo
我正在 react 应用程序中实现路由。我对react-router 4.0相对于react-router-redux的优势感到困惑。我也对react-router-dom感到困惑。 react-ro
我有以下两条路线:/items和/items/buy。 每个路线在一个 View 中对应一个选项卡。两条路线均使用精确的 Prop 进行渲染,但导航至/items/buy时,仍将两个选项卡标记为事件选
我有一个带有 url/product/0 的新产品页面。当使用按钮保存产品时,我想显示“已成功保存”消息并将 url 更改为/product/1024 而无需实际导航到该页面,因为这会导致对页面上的产
我对此有些困惑。我定义了一条名为classes/:id的路线。在应用程序中导航到该路线时,将调用componentDidMount()。但是,当重新加载页面或复制并粘贴URL时,页面将完全加载,但根本
假设我有以下2条路线: ... ... 现在,当我尝试按路线/news时,会触发带有Home参数的param1的根路线... 我认为解决方案是在像/?param1这
我正在将 Vue Router 与 Vue 3 一起使用,并尝试添加一个包罗万象的路由以在用户尝试访问无效 URL 时重定向用户。当我尝试使用通配符 (*) 时,我将以下错误记录到控制台: Uncau
我在我的项目中使用react-router-dom 4.0.0-beta.6。我有如下代码: 我想在 HomePage 组件中获取查询参数。我找到了 location.search 参数,它看起来像
我正在使用 Vue 路由器。在我的代码中,我曾经有: 这有效,并且页面数据被传递到组件。但是如何使用路由器 View 执行相同的操作呢?这似乎不起作用: js: var vm = new Vue(
我目前正在一个项目中使用react-router-redux,除了我不知道如何访问react-router路由器之外,一切都工作正常。 @connect((state, props) => {
当使用链接时,路由器按预期工作,尽管我收到警告 [history] pushState is deprecated;使用推送代替。 使用react-router-redux中的routeActio
我想创建一个多页 Meteor 网络和移动应用程序。因此,我正在寻找路由服务。 来自 research ,我收集了Iron Router就是我要找的。但是很多教程也提到了Meteor.Router I
我正在使用 React Router(版本 4.0.0)和 Router组件有一个名为“history”的必需 Prop 。 我知道在以前版本的react-router中你可以导入browserHis
我正在使用 氟罗包用于导航。 Fluro 和 Flutter 正在使用“路由器”。所以他们的类(class)合并了。我怎么能解决这个问题? lib/routers/routers.dart:2:1:
自从我更新了 Vue.js 应用程序的节点包后,我在浏览器控制台中收到以下警告: [vue-router] 's tag prop is deprecated and has beenremoved
我正在尝试在我的应用程序中将 react-router 更新到 v2.6 并将 react-router-relay 更新到 v0.7,但我正在努力按照变更日志来解决所有重大变更。我想我解决了所有的变
router-view 和 router-link 组件无法在我的应用中解析不知道是什么问题 附上应用代码它是一个主页应用程序,带有类似 navbar 的东西,用于路由到 vue 组件,如 state
我是一名优秀的程序员,十分优秀!