我正在构建一个相当简单的AngularJS应用程序,但为了简单起见,我宁愿不使用路由(如果可能的话),只是从服务器返回单个Angular页面.实质上,仅将Angular用于"窗口小部件"的布局和功能,而不是用于在页面之间移动.
话虽如此,我花了几个小时试图从当前URL中获取ID,因此:
/Tickets/:id
Run Code Online (Sandbox Code Playgroud)
我想在我的控制器中做的就是从URL获取ID,然后将其传递给Factory.(下面是使用伪代码的示例)
app.controller('ticketController', ['$scope', '$route',
function ($scope, $route, $location) {
//Get ID out of current URL
var currentId = someFunction().id;
factory.callFactory(ID);
}]);
Run Code Online (Sandbox Code Playgroud)
我也尝试过创建路由,但返回的对象似乎没有提供获取ID的方法.
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/tickets/:id',
{
templateUrl: '/partials/edit',
controller: 'ticketController'
}
);
}]);
Run Code Online (Sandbox Code Playgroud)
不知道我做错了什么.
asg*_*oth 42
尝试$routeParams改为:
app.controller('ticketController', ['$scope', '$routeParams',
function ($scope, $routeParams) {
//Get ID out of current URL
var currentId = $routeParams.id;
}]);
Run Code Online (Sandbox Code Playgroud)
Omi*_*-RH 18
如果您正在使用ui-router,请尝试$stateParams:
app.controller('ticketController', ['$scope', '$stateParams',
function ($scope, $stateParams) {
//Get ID out of current URL
var currentId = $stateParams.id;
}]);
Run Code Online (Sandbox Code Playgroud)