Angular2获取激活的路由参数和父参数

时间:2017-01-10 09:10:59

标签: angular angular2-routing angular2-components

如何从父组件和子路由获取RouteParams

if (request.getParameter("flag")!=null && request.getParameter("flag").equals("1")) {
    //Add your student in this block and show the input page again.
    response.sendRedirect("InputGrade.jsp");
}else  if (request.getParameter("flag")!=null && request.getParameter("flag").equals("2")) {
        //Add your student in this block and show the list.
        response.sendRedirect("ShowList.jsp");
}

在组件

export const routes: Routes = [
    { path: '', redirectTo: 'list', pathMatch: 'full' },
    { path: 'list', component: UsersComponent },
    {
        path: ':id', component: UserDetailComponent,
        children: [
            // { path: '', redirectTo: 'overview', pathMatch: 'full' },
            { path: 'overview', component: UserInfoComponent },
            { path: 'specs/:id', component: UserProfileComponent }
        ]
    }
];

但是当我的路线被激活时,我总是得到不确定。 请帮助解决这个问题?

提前致谢。

3 个答案:

答案 0 :(得分:5)

这是一个如何在.ts中获取参数值的简单示例, 之后,您可以根据自己的情况自定义

final AggregateValidationStatus aggregateValidationStatus = new AggregateValidationStatus(
        dataBindingContext.getBindings(), AggregateValidationStatus.MAX_SEVERITY);
aggregateValidationStatus.addChangeListener(new IChangeListener() {
    public void handleChange(ChangeEvent event) {
        //Here I have to trigger the MultiValidator to return either OK or ERROR
        boolean formIsValid = true;
        aggregateValidationStatus.getValue();
        for (Object o : dataBindingContext.getBindings()) {
            Binding binding = (Binding) o;
            IStatus status = (IStatus) binding.getValidationStatus().getValue();
            if (!status.isOK()) {
                formIsValid = false;
            }

            Control control = null;
            if (binding.getTarget() instanceof ISWTObservable) {
                ISWTObservable swtObservable = (ISWTObservable) binding.getTarget();
                control = (Control) swtObservable.getWidget();
            }
            if (binding.getTarget() instanceof CalendarComboObservableValue) {
                CalendarComboObservableValue observable = (CalendarComboObservableValue) binding.getTarget();
                control = (Control) observable.getControl();
            }

            if (binding.getTarget() instanceof IViewerObservable) {
                IViewerObservable observable = (IViewerObservable) binding.getTarget();
                control = observable.getViewer().getControl();
            }

            ControlDecoration decoration = decoratorMap.get(control);
            if (decoration != null) {
                if (status.isOK() || status.matches(Status.WARNING)) {
                    decoration.hide();
                } else {
                    decoration.setDescriptionText(status.getMessage());
                    decoration.show();
                }
            }
        }
        if (saveBtn != null)
            saveBtn.setEnabled(formIsValid);
    }
});

答案 1 :(得分:4)

您需要ActivatedRouteSnapshot来遍历激活的路线。

https://angular.io/docs/ts/latest/api/router/index/ActivatedRouteSnapshot-interface.html

答案 2 :(得分:0)

var myParamValue = this.route.snapshot.queryParams['myParam'];