我创建了一个使用 Angular 1.0.8 的项目。我创建了项目,正确设置了我的路由和我的控制器,当我尝试访问路由中的 $scope 时,它给了我一个问题,我必须去访问我使用$scope.$$prevSibling[variables设置的范围变量]我是不是设置错了??我创建了一个具有相同结构的类似项目,它工作正常我可以直接使用我的模块访问 $scope 吗?
function AppCtrl($scope) {
$scope.navigation = [
{ name: "main", route: "#/" },
{ name: "edit", route: "#/edit" },
{ name: "save", route: "#/save" },
{ name: "settings", route: "#/settings" }
];
$scope.currentPage = null;
$scope.setCurrentPage = function (index) {
$scope.currentPage = $scope.navigation[index];
}
angular.forEach($scope.navigation, function (value, key) {
$scope[value.name + "Active"] = "";
})
$scope.setActive = function (name) {
angular.forEach($scope.navigation, function (value, key) {
$scope[value.name + "Active"] = "";
})
$scope[name + "Active"] = "active";
}
}
这是我的模块设置:
angular.module('Sample', [])
.config(appRouter);
function appRouter($routeProvider) {
$routeProvider.when("/", {
templateUrl: "partials/default.html",
controller: function ($scope) {
console.log($scope.$$prevSibling.setActive);
}
})
}
<!DOCTYPE html>
<html ng-app="Sample">
<head>
<title>Sample Angular App</title>
<script src="Scripts/lib/basket.js"></script>
<link rel="stylesheet" href="css/global.css"/>
<link rel="stylesheet" href="css/style.css" />
<script src="Scripts/lib/angular.min.js"></script>
<script src="Scripts/controllers/app.js"></script>
<script src="Scripts/app.js"></script>
</head>
<body>
<div class="wrapper" ng-controller="AppCtrl">
<div class="navigation">
<ul class="cf">
<li ng-repeat="link in navigation" ng-class="{active:$index == activeIndex}" class="{{link.name}}"><a class="icon-{{link.icon}}" href="{{link.route}}"> {{link.name | uppercase}} </a></li>
</ul>
</div>
<div class="viewWrapper">
<div class="sidebarParent">
<div ng-include src="sidebarUrl" class="sidebar">
</div>
</div>
<div class="viewParent">
<div ng-view class="view">
</div>
</div>
</div>
</div>
</body>
</html>
style.css 用于我的图标字体,global.css 用于我的主要样式,basketjs 用于在本地存储一些脚本,以便我以后可以使用easeljs,ng-repeat 用于构建导航。一切正常,但只是范围关闭了?我必须去prevSibling?controllers/app.js 是 AppCtrl 所在的位置,而 Scripts/app.js 是我的模块所在的位置。