我想使用$locationProvider
AngularJS 中的服务,无需在我的中渲染新的 HTML 模板ng-view
.
我有一个div
通过按需显示的元素ng-show
绑定到数据元素的存在。我想通过以下方式将其连接到浏览器位置$locationProvider
但保持在同一个模板内。
模板:
<div> other stuff ... </div>
<div ng-show="model">{{ model.element }}</div>
控制器:
Controller = function($scope, $location) {
$scope.show = function() {
$scope.model = model; // show div
}
$scope.hide = function() {
$scope.model = null; // hide div
}
}
我不知道如何整合$location
服务于此。如果设置了 AngularJS 也会覆盖该位置history.pushState
直接地。
使用来源,卢克:-)http://code.angularjs.org/1.0.0/angular-1.0.0.js http://code.angularjs.org/1.0.0/angular-1.0.0.js
如果您查看 ngViewDirective (实际上非常简单),它只会捕获“$routeChangeSuccess”事件并相应地更改视图。
因此,您可以捕获 $routeChangeSuccess ,然后将 $route 注入您的控制器,检查新路线是否是您想要的路线,并相应地显示。 (我认为:D)
这可能有效(未经测试):
//declare a route so angular knows to broadcast about it when it's hit
//We aren't declaring any actions for the route though,
//since below we define custom actions
myApp.config(function($routeProvider) {
$routeProvider.when('/superman', {});
});
function SupermanCtrl($scope, $route) {
$scope.$on('$routeChangeSuccess', function() {
//If this doesn't work, console.log $route.current to see how it's formatted
if ($route.current == '/superman')
$scope.show = true;
else
$scope.show = false;
});
}
我希望它能起作用,如果不起作用,那也应该足够开始了。我鼓励您阅读 ngViewDirective,如果这不起作用,请搜索“$routeChangeSuccess”并找出它如何/为何被广播。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)