Javascript 如何访问Angular.JS中$routeProvider配置中的factory数据对象?
我正在尝试创建Angular.JS计算器,如果某个factory数据对象为空,它会将用户返回到根页面。我的问题是,我无法理解如何访问Javascript 如何访问Angular.JS中$routeProvider配置中的factory数据对象?,javascript,angularjs,Javascript,Angularjs,我正在尝试创建Angular.JS计算器,如果某个factory数据对象为空,它会将用户返回到根页面。我的问题是,我无法理解如何访问数据.Terms对象,以查看$routeProvider配置中/condition路径下的$routeProvider对象是否为空 在下面的代码中,如果(!Data.Terms)不起作用,则说明的部分无效。我希望它返回存储在factory Data函数中的术语值: // Creating the module var myApp = angular.module("
数据.Terms
对象,以查看$routeProvider
配置中/condition
路径下的$routeProvider
对象是否为空
在下面的代码中,如果(!Data.Terms)不起作用,则说明的部分无效。我希望它返回存储在factory Data函数中的术语值:
// Creating the module
var myApp = angular.module("app-calculator", ["calculatorControls", "ngRoute"])
.config(function ($routeProvider) {
$routeProvider.when("/", {
controller: "termsController",
controllerAs: "vm",
templateUrl: "/content/templates/termsView.html"
});
$routeProvider.when("/condition", {
controller: "conditionController",
controllerAs: "vm",
templateUrl: "/content/templates/conditionView.html",
resolve: {
mess: function ($location) {
// Check if the Data.Terms field has been completed,
// if not then return to the root path.
if (!Data.Terms) {
$location.path('/');
}
//
}
}
});
// my Data
myApp.factory('Data', function () {
var Terms = '';
return {
Terms: Terms,
}
})
您应该将数据作为依赖项注入解析部分
var myApp = angular.module("app-calculator", ["calculatorControls", "ngRoute"])
.config(function ($routeProvider) {
$routeProvider.when("/", {
controller: "termsController",
controllerAs: "vm",
templateUrl: "/content/templates/termsView.html"
});
$routeProvider.when("/condition", {
controller: "conditionController",
controllerAs: "vm",
templateUrl: "/content/templates/conditionView.html",
resolve: {
mess: function ($location, Data) {
// Check if the Data.Terms field has been completed,
// if not then return to the root path.
if (!Data.Terms) {
$location.path('/');
}
//
}
}
});
尝试一下mess:function($location,Data){
这就成功了。谢谢你,克莱斯!在回答中发布它,我会把它标记为接受。