目前在app.js我有以下路线:
var gm = angular.module('gm', ['gm.services','gm.directives','gm.filters','gm.controllers','ngSanitize']);
gm.config(['$routeProvider', 'Path', function($routeProvider, Path) {
$routeProvider.when('/login', {
templateUrl: Path.view('application/authentication/login.html'),
controller: 'authController'
});
$routeProvider.when('/dashboard', {
templateUrl: Path.view('application/dashboard/index.html'),
controller: 'dashboardController'
});
$routeProvider.otherwise({
redirectTo: '/login'
});
}]);
Run Code Online (Sandbox Code Playgroud)
我正在尝试注入Path依赖项,如您所见.虽然我收到一个错误,说它找不到这个提供商.我认为这是因为配置模块提供程序首先执行.下面是我在"services.js"中的路径提供程序定义
gm.factory("Path", function() {
return {
view: function(path) {
return 'app/views/' + path;
},
css: function(path) {
return 'app/views/' + path;
},
font: function(path) {
return 'app/views/' + path;
},
img: function(path) {
return 'app/views/' + path;
},
js: function(path) {
return 'app/views/' + path;
},
vendor: function(path) {
return …Run Code Online (Sandbox Code Playgroud) 尝试为模块设置一些帮助器值.尝试服务和价值,但没有帮助:
var finance = angular.module('finance', ['finance.services'])
.value("helpers", {
templatePath: function (name) {
return '/areas/scripts/finance/templates/' + name + '/index.html';
}
})
.config(['$routeProvider', 'helpers', function ($routeProvider, helpers) {
$routeProvider.
when('/', {
templateUrl: helpers.getTemplatePath('dashboard'),
controller: DashboardController
})
.when('/people', {
templateUrl: '/areas/scripts/app/people/index.html',
controller: PeopleController
})
.otherwise({
redirectTo: '/dashboard'
});
}]);
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
wikiApp.config(['$routeProvider','authService',
function($routeProvider,authService) {
var admin = authService.getLoggedin();
$routeProvider
.when('/hjem',{
templateUrl: 'partials/homeContent.html',
admin: false
})
.when('/articles/:article',{
templateUrl: 'partials/articles.html',
admin: false
})
.when('/newArticle',{
templateUrl: 'partials/postArticle.html',
controller: 'articleController',
admin: true
})
Run Code Online (Sandbox Code Playgroud)
authService.getLoggedin()返回false或true,具体取决于用户是否登录.如果他们不被允许,我想不允许他们到Url.
但我收到此错误:错误:[$ injector:modulerr]由于以下原因无法实例化模块wikiApp:[$ injector:unpr]未知提供者:authService
我需要$http在config方法中使用service.但我无法$http在配置中使用,我收到unknown Provider错误消息.
我的代码:
.config(function($http, $routeProvider, $provide) {
$http.get("sampleslist.js").success(function(data) {
var loop = 0, currentRoute;
for (loop = 0; loop < data[loop].pages.length; loop++) {
currentRoute = data[loop].pages;
var routeName = "/" + currentRoute[loop].name;
$routeProvider.when(routeName, {
templateUrl:"/" + currentRoute.name + ".html",
})
}
})
app = {controller: $controllerProvider.register}
})
Run Code Online (Sandbox Code Playgroud)
你能为此提供解决方案吗?
我在我的角度应用程序中使用angular-route-segment并尝试从json feed配置段.
我已经有这个问题,因为我无法弄清楚如何注入$http到app.config功能.这失败了Unknown provider: $http
myApp.config(["$http", "$routeSegmentProvider", function ($http, $routeSegmentProvider) {
/* setup navigation here calling $routeSegmentProvider.when a number of times */
}
Run Code Online (Sandbox Code Playgroud)
因此,而不是注入$ HTTP到config,我也尝试注入$routeSegmentProvider到myApp.run
myApp.run(["$http", "$routeSegment", function($http, $routeSegment) {
/* can use $http here to get data, but $routeSegment is not the same here */
/* $routeSegment does not have the when setup method */
}]);
Run Code Online (Sandbox Code Playgroud)
我也试过了
myApp.run(["$http", "$routeSegmentProvider", function($http, $routeSegmentProvider)
Run Code Online (Sandbox Code Playgroud)
但我明白了 Unknown provider: $routeSegmentProviderProvider <- $routeSegmentProvider
将服务注入子状态解析功能时获得未知提供程序.但如果在父状态中定义了一个解决方案,它就可以正常工作.下面是一些示例代码:
我定义了一个服务模块
angular.module('services', [])
.factory('myService', function() {
// my service here
})
Run Code Online (Sandbox Code Playgroud)
并初始化应用程序
var app = angular.module('app', ['services', 'ui.router']);
app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider,
$urlRouterProvider) {
$stateProvider.state('wizard', {
url: '/wizard',
abstract: true
})
.state('wizard.step1', {
url: '/step1',
templateUrl: ... ,
resolve: {
name: function(myService) {
// do something with mySerice
}
},
controller: function(name) {
// controller codes here
}
})
}]);
Run Code Online (Sandbox Code Playgroud)
我在wizard.step1解决方案中收到错误未知提供程序抱怨myService的错误.但是,如果我在父状态中添加随机解析,就像
$stateProvider.state('wizard', {
url: '/wizard',
abstract: true,
resolve: {
a: function() { return 1; }
}
})
Run Code Online (Sandbox Code Playgroud)
然后它可以正常工作.不知道这里发生了什么
我不确定我是否正确接近这个但我正在建立一个电子商务网站 - 该网站的一部分有6个不同的产品网格页面,每个网页都可以使用相同的视图:
<ul class="products row">
<li class="product thumbnail col-1 grid" ng-repeat="whisky in whiskies | orderBy: sort">
<p class="picture">
<a ng-href="#/json/{{whisky.id}}"><img ng-src="images/scotch/{{whisky.imageUrl}}" alt="{{whisky.name}}"/></a>
</p>
<div class="desc">
<h2>{{whisky.name}}</h2>
<p class="price"><span>£{{whisky.price}}</span></p>
</div>
<div class="actions">
<button class="add-to-basket btn btn-primary btn-medium fa fa-shopping-cart" data-item='{"imageUrl" : "{{whisky.imageUrl}}", "price" : "{{whisky.price}}", "startPrice" : "{{whisky.price}}", "name" : "{{whisky.name}}", "totalItems" : "{{1}}"}' ng-click="updateMiniBasket($event)"></button>
</div>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
和相同的控制器:
whiskyControllers.controller('whiskyListCtrlr', ['$scope', 'whiskyList', '$http',
function($scope, whiskyList, $http){
whiskyList.getWhiskies().then(function(d) {
$scope.whiskies = d.data;
})
}
])
Run Code Online (Sandbox Code Playgroud)
但需要在路线提供者配置中有不同的路线,即一个去苏格兰威士忌,一个去爱尔兰,一个去日本等.
如何对路由进行编码,以便不同的页面共享同一个控制器和视图?是否可以将参数从路由器传递到控制器?
非常感谢