GVKun编程网logo

如何在不做路由的情况下在AngularJS中设置查询参数?(angular实现查询功能)

23

在本文中,我们将带你了解如何在不做路由的情况下在AngularJS中设置查询参数?在这篇文章中,我们将为您详细介绍如何在不做路由的情况下在AngularJS中设置查询参数?的方方面面,并解答angul

在本文中,我们将带你了解如何在不做路由的情况下在AngularJS中设置查询参数?在这篇文章中,我们将为您详细介绍如何在不做路由的情况下在AngularJS中设置查询参数?的方方面面,并解答angular实现查询功能常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的angularjs – Angular ui路由器解析查询参数到布尔值、angularjs – Angular2 $location.search()等价物(设置查询参数)、angularjs – 使用Angular UI-Grid,如何在不使用行选择的情况下访问行实体数据?、angularjs – 使用angular-ui-route限制用户在不登录的情况下进入内页

本文目录一览:

如何在不做路由的情况下在AngularJS中设置查询参数?(angular实现查询功能)

如何在不做路由的情况下在AngularJS中设置查询参数?(angular实现查询功能)

我的Angular应用程序允许用户加载项目,当发生这种情况时,我想设置一个包含该项目ID的查询字符串,以便如果用户刷新页面(或想要链接到该页面),则该URL已经存在设置(如果$
routeParams中存在ID,则已经有代码可以加载该项目)。

如何设置该查询参数而不引起路由?如果发生路由,页面上还有其他东西会重置(包括必须重新加载其所有数据的插件),因此仅更改查询参数很重要。

简而言之,当我加载项目123时,我要做的就是URL从以下位置更改:

www.myurl.com/#/Items

至:

www.myurl.com/#/Items?id=123

没有进行任何路由。

最好的方法是什么?

答案1

小编典典

在$
routeProvider配置中设置reloadOnSearch为false:

$routeProvider  .when(''/items'', {    controller: ''ItemsCtrl'',    templateUrl: ''/templates/items'',    reloadOnSearch: false  },  ...);

并在控制器中用于$location.search()设置id参数:

$location.search(''id'', 123);

angularjs – Angular ui路由器解析查询参数到布尔值

angularjs – Angular ui路由器解析查询参数到布尔值

考虑具有查询参数的状态的这种情况.我想将它们声明为标志,以便我可以在控制器中获取并获取true,false或undefined

$stateProvider.state('foo.bar',{
         url: '/foobar?flag1&flag2',templateUrl: 'foo/bar/template.tpl.html',controller: 'FooBarCtrl'
});

myModule.controller('FooBarCtrl',function($stateParams){
         $stateParams.flag1 <<--- is string but can it be of type bool?
         $stateParams.flag2 <<--- is string but can it be of type bool?

});

一些URL示例:

/foobar?flag1=true    -->> should yield {flag1: true,flag2: undefined}
/foobar?flag2=false    -->> should yield {flag1: undefined,flag2: false}
/foobar?flag1=false&flag2=true    -->> should yield {flag1: false,flag2: true}
/foobar?flag1=1&flag2=0    -->> should yield {flag1: true,flag2: false}

etc...

目前$stateParams只提供字符串.有没有让路由器解析params作为标志?这比在控制器中手动解析要优雅得多.

解决方法

您应该能够将bool用作类型

url: '/foobar?{flag1:bool}&{flag2:bool}',

但我们甚至可以使用我们的自定义类型(让我们称之为boolean):

app.config(['$urlMatcherFactoryProvider',function($urlMatcherFactory) {
  $urlMatcherFactory.type('boolean',{
 name : 'boolean',decode: function(val) { return val == true ? true : val == "true" ? true : false },encode: function(val) { return val ? 1 : 0; },equals: function(a,b) { return this.is(a) && a === b; },is: function(val) { return [true,false,1].indexOf(val) >= 0 },pattern: /bool|true|0|1/
})
}]);

url的状态def将是这样的:

url: '/foobar?{flag1:boolean}&{flag2:boolean}',

这应该工作:

<a ui-sref="foobar({ flag1: 0,flag2:true })">
<a ui-sref="foobar({ flag1: 1,flag2:false })">

这是plunker with example

angularjs – Angular2 $location.search()等价物(设置查询参数)

angularjs – Angular2 $location.search()等价物(设置查询参数)

我的Web应用程序有多个过滤器,应该在当前的Url中表示,以便用户可以简单地复制/书签当前页面以便以后将其恢复.

在angular1中,我使用$location.search(name,value)来简单地在变化时设置seach参数.现在我想在angular2中得到类似的东西.

还是错了?

解决方法

我认为你必须在Angular2中使用路由器.代码示例:

import {Component} from 'angular2/core';
import {RouteConfig,ROUTER_DIRECTIVES} from 'angular2/router';
import {ProfileComponent} from './profile.component';

@Component({
    selector: 'my-app',template: `
    <router-outlet></router-outlet>
    `,directives: [ROUTER_DIRECTIVES]
})

@RouteConfig([
  {path: '/profile/:id',name: 'Profile',component: ProfileComponent}
])

export class AppComponent {
}

The :id is a route parameter and you can do an URL like that:
/profile/2 and 2 is the value of the id parameter.

您可以在Angular2 doc:router doc中找到更多详细信息

angularjs – 使用Angular UI-Grid,如何在不使用行选择的情况下访问行实体数据?

angularjs – 使用Angular UI-Grid,如何在不使用行选择的情况下访问行实体数据?

我正在尝试从Smart-Table版本1.x切换到Angular ui-grid(版本3.0),替换ng-grid.
我几乎喜欢关于ui-grid的一切,但有一件事让我发疯.在智能表中,daTarow有一个值,这是一种在表中引用实体的便捷方式.

我正在使用它填充html模板以包含来自实体的字段信息,例如放置在网格单元格内的html模板中的ng-click =“$parentScope.edit(daTarow.id)”.

但是,在ui-grid中,我似乎无法在不进行正式行或单元格选择的情况下访问实体对象.将它包含在单元格模板中的任何努力都会产生一个对象(row.entity)但我无法访问任何实体元素,它们显示为未定义.有任何想法吗?

此外,我已经能够在html模板中执行一个方法,但只能在没有参数的情况下执行,而不是尝试使用实体本身的参数.

这是我使用smart-table的html模板:

<a data-toggle="tooltip" data-placement="top" title="View    {{filteredRowCollection}}"   ng-click="$parent.$parent.$parent.$parent.view(daTarow.id)">
</a>
<a data-toggle="tooltip" data-placement="top" title="Edit {{selectionId}}" ng-click="grid.appScope.edit(row.entity.id)">
</a>
<a data-toggle="tooltip" data-placement="top" title="Delete {{selectionId}}"  ng-click="$parent.$parent.$parent.$parent.delete(daTarow.id)">
</a>

我试图用ui-grid这样的东西:

function edit(row){
    . . .
};

行,此时是一个对象,就像row.entity一样.我希望能够使用像row.entity.id这样的字段之一,但它是未定义的.

解决方法

这篇文章很有用,https://technpol.wordpress.com/2014/08/23/upgrading-to-ng-grid-3-0-ui-grid/

基本上,您需要为网格设置外部范围,以便您可以访问数据.

angularjs – 使用angular-ui-route限制用户在不登录的情况下进入内页

angularjs – 使用angular-ui-route限制用户在不登录的情况下进入内页

我有一个有角度的项目.如果我在网址中键入“仪表板”,则会重定向到“dashboard.html”页面.但我想限制用户进入该页面而未成功登录.

在我的路由代码下面请检查.

index.js

angular.module('adminsuite',['ui.router','ngCookies']).config(function($stateProvider,$urlRouterProvider) {

$urlRouterProvider.otherwise('/');

$stateProvider
    .state('login',{
        url: '/',views:{
            pageContent:{
                templateUrl: 'Login/login.html',controller: 'loginController'
            },footer:{
                templateUrl: 'common/footer.html',controller: 'footerController'
            }
        }



    })
    // HOME STATES AND nesTED VIEWS ========================================
     .state('dashboard',{
        url: '/dashboard',views:{
            header:{
                templateUrl: 'common/header.html',controller: 'headerController'
            },pageContent:{
                templateUrl: 'dashboard/dashboard.html',controller: 'dashboardController'
            },controller: 'footerController'
            }
        }
    })
    //SURVEY STATES
    .state('survey',{
        url: '/survey',views:{
            header:{
                templateUrl: 'common/headerTool.html',pageContent:{
                templateUrl: 'survey/survey.html',controller: 'surveyController'
            },controller: 'footerController'
            }
        }
    });

    // ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================

});

解决方法

如果用户成功登录,则在$rootscope中的loginController存储值中.

if(userloggedin == true )
{
  $rootscope.loggedinuser = true
}

在您的dashboardController中从$rootscope获取值并检查$rootscope.loggedinuser是否等于true然后dasboard.html页面将显示否则页面重定向到登录页面.

在您的dashboardController中

if($rootscope.loggedinuser !=true)
{
    $state.go('login');
}

今天关于如何在不做路由的情况下在AngularJS中设置查询参数?angular实现查询功能的分享就到这里,希望大家有所收获,若想了解更多关于angularjs – Angular ui路由器解析查询参数到布尔值、angularjs – Angular2 $location.search()等价物(设置查询参数)、angularjs – 使用Angular UI-Grid,如何在不使用行选择的情况下访问行实体数据?、angularjs – 使用angular-ui-route限制用户在不登录的情况下进入内页等相关知识,可以在本站进行查询。

本文标签: