Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了4、Angular-Ui Router URL路由(完结篇)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。


@H_508_3@most states in your application will probably have a url associated with them. URL RoutIng was not an afterthought to the state mechanics,but was figured into the design from the beginning (all while keeping states separate from url routIng)
应用程序中的大多数状态可能会有一个与之关联的 url。url 路由不是对状态机制的事后虑,而是从一开始就被设计到了设计中 (所有的状态都保持与 url 路由分开)

Here's how you set a basic url.
下面是如何设置基本url的方法

$stateProvider
    .state('contacts',{
        url: "/contacts",templateUrl'contacts.html'
    })

Now when the user accessesindex.html/contactsthen the 'contacts' state would become active and the mainui-viewwill be populated with the 'contacts.html' partial. Alternatively,if the user were to transition to the 'contacts' state viatransitionTo('contacts')then the url would be updated toindex.html/contacts
用户访问 index.html/contacts时。然后,“contacts”状态将变为活动,ui-view将被“contacts.html”填充。或者,如果用户通过transitionTo("contacts")切换到“contacts”状态,那么url将被更新为index.html/contacts

URL Parameters
URL参数

Basic Parameters
基本参数

Often,URLs have dynamic parts to them which are called parameters. There are several options for specifying parameters. A basic parameter looks like this:
通常,UR有动态的部分,它们被称为参数。有几个选项可以指定参数。一个基本的参数是这样的:

'contacts.detail',98);">"/contacts/:contactId",98);">'contacts.detail.html',controller: function ($stateParams) { // If we got here from a url of /contacts/42 expect($stateParams).toBe({ContactId"42"}); } })

Alternatively you can also use curly brackets:
或者你也可以使用花括号:

// identical to prevIoUs example url"/contacts/{ContactID}"

Examples:
示例:

  • '/Hello/'- Matches only if the path is exactly '/Hello/'. There is no special treatment for Trailing slashes,and patterns have to match the entire path,not just a prefix.
    '/Hello/'-只有在路径完全是“/Hello/”的情况下才匹配。对于后面的斜杠没有特殊的处理,并且模式必须匹配整个路径,而不仅仅是一个前缀。
  • '/user/:id'- Matches '/user/bob' or '/user/1234!!!' or even '/user/' but not '/user' or '/user/bob/details'. The second path segment will be captured as the parameter 'id'.
    '/user/:id' - 匹配 '/user/bob' 或 '/user/1234!!!' 或者是 '/user/' 但不是 '/user' 或 '/user/bob/details'. 第二个路径段将被捕获为参数'id'。
  • '/user/{iD}'- Same as the prevIoUs example,but using curly brace Syntax.
    '/user/{iD}' - 与前面的示例相同,但使用的是花括号语法。
  • '/user/{id:int}'- The param is interpreted as Integer.
    '/user/{id:int}' - id参数被解释为整数。

Note:
注意

  • Parameter names may contain only word characters (laTin letters,digits,and underscore) and must be unique within the pattern (across both path and search parameters).
    参数名称只能包含字母、数字和下划线,并且在模式中必须是唯一的 (在路径和搜索参数中)。

Using Parameters in Links
链接中使用参数

To create a link that passes parameters,use the state name like a function and pass it an object with parameter names as keys. The properhrefwill be generated.
要创建传递参数的链接,可以使用状态名作为函数,并传递带有参数名的对象作为键。将生成适当的href。

For example,using the above state which specified acontactIdparameter,create a link like so:
例如,使用指定了 contactId 参数的上述状态,创建如下链接:

<a ui-sref="contacts.detail({ContactId: iD})">View Contact</a>

The value foridcan be anything in scope.
id的值可以是任意值。

Regex Parameters
参数正则表达式

A bonus to using curly brackets is the ability to set a Regular Expression rule for the parameter:
使用花括号的一个好处是可以为参数设置正则表达式规则:

// will only match a contactId of one to eight number characters url"/contacts/{ContactId:[0-9]{1,8}}"

Examples:示例:

    '/user/{id:[^/]*}'- Same as'/user/{iD}'from the prevIoUs example.
    '/user/{id:[^/]*}' - 与前一个示例中的“/user/id”相同。
  • '/user/{id:[0-9a-fA-F]{1,8}}'- SIMILAR TO the prevIoUs example,but only matches if the id parameter consists of 1 to 8 hex digits.
    '/user/{id:[0-9a-fA-F]{1,8}}' - 与前面的示例类似,但是只有当id参数@R_259_11262@到8十六进制数字时才匹配。
  • '/files/{path:.*}'- Matches any URL starTing with '/files/' and captures the rest of the path into the parameter 'path'.
    '/files/{path:.*}' - 匹配任何以'/files/'开头的URL,并将其余路径捕获到参数“path”中。
  • '/files/*path'- Ditto. Special Syntax for catch all.

Warning:
警告

  • Don't put capturing parentheses into your regex patterns,the UrlMatcher in ui-router adds those itself around the entire regex. You're effectively introducing a second capture group for the same parameter,which trips up the numbering in the child URl. You can use non-capturing groups though,i.e. (?:...) is fine.
    不要在正则表达式模式中使用捕获括号,ui-router中的UrlMatcher在整个正则表达式中添加了这些括号。您可以有效地为相同的参数引入第二个捕获组,该参数将在子URL中访问编号。也就是说您可以使用非捕获组。
  • Regular expression can't include forWARD slashes as that's route segment delimiter
    正则表达式不能包含斜杠,因为这是路由分隔符
  • Route parameters with regular expressions can't be optional or greedy
    带有正则表达式的路由参数不能是可选的或贪婪的

Query Parameters
查询参数

You can also specify parameters as query parameters,following a '?':
还可以将参数指定为查询参数,即'?':

url"/contacts?myParam" // will match to url of "/contacts?myParam=value"

If you need to have more than one,separate them with an '&':
如果你需要不止一个查询参数,则把它们用"&"分开。

"/contacts?myParam1&myParam2" // will match to url of "/contacts?myParam1=value1&myParam2=wowcool"

Using Parameters without Specifying Them in State URLs
使用参数而不用在状态URL中指定它们

You still can specify what parameters to receive even though the parameters don't appear in the url. You need to add a new field params in the state and create links as specified in Using Parameters in Links
即使参数没有出现在url中,您仍然可以指定要接收的参数。您需要在状态中添加一个新的字段params,并在指定的链接中使用参数。

Box-sizing: border-Box; margin-top: 0px; margin-bottom: 16px; color: rgb(36,you have the state.
例如,你的状态。

.Box-sizing: border-Box; color: rgb(111,params: { param1: @H_175_262@null },"Segoe UI Symbol"; font-size: 16px;">The link you create is
你创建的链接

<a ui-sref="contacts({param1: value1})">View Contacts</a>

Or you can pass them to $state.go() too.
你也可以在$state.go()方法中使用参数

$state.go('contacts',{param1: value1})

URL RoutIng for nested States
嵌套状态的URL路由

Appended Routes (default)
附加路由(认)

When using url routIng together with nested states the default behavior is for child states to append their url to the urls of each of its parent states.
当使用url路由与嵌套状态一起使用时,认的行为是让子状态将url附加到每个父状态的url。

$stateProvider .Box-sizing: border-Box; color: rgb(111,{ url'/contacts',... }) .'contacts.list',98);">'/list',73);">...
});

So the routes would become:
所以路由会变成:

  • 'contacts' state@H_405_12@matches"/contacts"
    'contacts'状态匹配"/contacts"
  • 'contacts.list' state@H_405_12@matches"/contacts/list". The urls were combined.
    'contacts.list'状态匹配"/contacts/list"

Absolute Routes (^)
绝对路由(^)

If you want to have absolute url matching,then you need to prefix your url String with a special symbol '^'.
如果你想要有绝对的url匹配,那么你需要在你的url字符串前面加上一个特殊的符号 '^'

'^/list',0.05); border-radius: 3px;">"/contacts"
'contacts' 状态匹配 "/contacts"
  • 'contacts.list' state@H_405_12@matches"/list". The urls werenotcombined because^was used.
    url'contacts.list' 状态匹配"/list",现在url没有被组合在一起,因为使用了'^'符号。
  • $stateParams service
    $stateParams服务

    As you saw prevIoUsly the $stateParams service is an object that will have one key per url parameter. The $stateParams is a perfect way to provide your controllers or other services with the individual parts of the navigated url.
    正如您之前看到的,$stateParams服务是一个对象,每个url参数都有一个键。$stateParams是为您的控制器或其他服务提供导航url的各个部分的完美方式。

    Note:$stateParams service must be specified as a state controller,and it will be scoped so only the relevant parameters defined inthatstate are available on the service object.
    注意:$stateParams服务必须指定为状态控制器为作用域,因此只有在该状态中定义的相关参数在服务对象中可用。

    // If you had a url on your state of:
    url: '/users/:id/details/{typE}/{repeat:[0-9]+}?from&to'
    
    // Then you navigated your browser to:
    '/users/123/details//0'
    
    // Your $stateParams object would be
    { id:'123',type:'',repeat:'0' }
    
    // Then you navigated your browser to:
    '/users/123/details/default/0?from=there&to=here'
    
    // Your $stateParams object would be
    { id:'123',type:'default',repeat:'0',from:'there',to:'here' }

    Important$stateParamsGotcha
    $stateParams主要问题

    In state controllers,the$stateParamsobject will only contain the params that were registered with that state. So you will not see params registered on other states,including ancestors.
    在状态控制器中,$stateParams对象只包含在该状态注册的参数。所以你不会看到参数在其他状态中注册包括祖先状态。

    $stateProvider.Box-sizing: border-Box; color: rgb(111,{ url'/contacts/:contactId',73);">function($stateParams){ $stateParams.contactId //*** Exists! ***// } }).'contacts.detail.subitem',98);">'/item/:itemId',73);">function($stateParams){ $stateParams.contactId //*** Watch Out! DOESN'T EXIST!! ***// $stateParams.itemId //*** Exists! ***// } })

    Instead,use a resolve statement in the parent route.
    相反,在父路由中使用解析(resolvE)语句。

    //*** Exists! ***// },resolve:{ contactId: ['$stateParams',73);">function($stateParams){ return $stateParams.contactId; }] } }).function($stateParams,contactId){ contactId //*** Exists! ***// $stateParams.itemId //*** Exists! ***// } })

    $urlRouterProvider

    $urlRouterProvider has the responsibility of watching $LOCATIOn. When $LOCATIOn changes it runs through a list of rules one by one until a match is found. $urlRouterProvider is used behind the scenes anytime you specify aurlin a state configuration. All urls are compiled into a UrlMatcher object (see $urlMatcherFactory below).
    $urlRouterProvider有责任观察$LOCATIOn。当$LOCATIOn改变时,它会遍历规则列表直到找到匹配为止。当您在状态配置中指定url时,$urlRouterProvider在后台所所有的url都被编译成一个UrlMatcher对象(请参阅下面的$urlMatcherFactory)。

    There are several methods on $urlRouterProvider that make it useful to use directly in your module config.
    $urlRouterProvider上有几个方法,可以使它在模块配置中直接使用。

    when()for redirection

    Parameters:
    参数:

    handler作为字符串类型时

    If handler is a String,it is treated as a redirect,and is interpolated according to the Syntax of match (i.e. like String.replace() for RegExp,or like a UrlMatcher pattern otherwisE).
    如果handler是字符串,则将其视为重定向,并根据match的语法进行插值(例如,用于RegExp的String.replace(),或者类似于UrlMatcher模式)。

    app.config(function($urlRouterProvider){ // when there is an empty route,redirect to /index $urlRouterProvider.when('','/index'); // You can also use regex for the match parameter $urlRouterProvider.when(/aspx/i,98);">'/index'); })

    handler作为函数

    If the handler is a function,it is injectable. It gets invoked if $LOCATIOn matches. You have the option of inject the match object as $match
    如果handler是一个函数,它是可注入的。如果$LOCATIOn匹配,就会调用它。您可以选择将匹配对象注入$match

    handler可以返回::

    • falsyto inDicate that the rule didn't match after all,then $urlRouter will conTinue trying to find another one that matches.
      falsy表明这条规则根本不匹配,于是url路由器将继续寻找匹配的另一个
    • aString,which is treated as a redirect and passed to $LOCATIOn.url()
      一个字符串,它被视为重定向,并传递给$LOCATIOn.url()
    • nothingor anytruthyvalue tells $urlRouter that the url was handled
      nothing 或任何truthy值,意思告诉 $urlRouter 该 url 已被处理

    Here's the actual code that we use to register state's that have urls behind the scenes.
    下面是我们用来注册状态的代码,在后台有url。

    $urlRouterProvider.when(state.url,['$match',73);">function ($match,$stateParams) { if ($state.$current.navigable != state || !equalForKeys($match,$stateParams)) { $state.transitionTo(state,$match,false); } }]);

    Parameters:
    参数

      pathString | FunctionThe url path you want to redirect to or a functionrulethat returns the url path. The function version is passed two params:$injectorand$LOCATIOn.
      path String | Function 您想要重定向的url路径,或者返回url路径的函数。函数版本有两个参数:$injector和$LOCATIOn。
    // if the path doesn't match any of the urls you configured // otherwise will take care of routIng the user to the specified url $urlRouterProvider.otherwise(// Example of using function rule as param $urlRouterProvider.otherwise(function($injector,$LOCATIOn){ ... some advanced code... }); })

    handlerFunctionA function that takes in the $injector and $LOCATIOn services as arguments. You are responsible for returning a valid path as a String.
    handler Function 一个函数,它接受$injector和$LOCATIOn服务作为参数。函数需要返回一条有效字符串路由。

    function ($urlRouterProvider) { // Here's an example of how you might allow case insensitive urls // Note that this is an example,and you may also use // $urlMatcherFactory.caseInsensitive(true); for a similar result. $urlRouterProvider.rule(function ($injector,$LOCATIOn) { //what this function returns will be set as the $LOCATIOn.url var path = $LOCATIOn.path(),normalized = path.toLowerCase(); if (path != normalized) { //instead of returning a new url String,I'll just change the $LOCATIOn.path directly so I don't have to worry about construcTing a new url String and so a new state change is not triggered $LOCATIOn.replace().path(normalized); } // because we've returned nothing,no state change occurs }); })

    $urlMatcherFactory and UrlMatchers
    $urlMatcherFactory 和 UrlMatchers

    Defines the Syntax for url patterns and parameter placeholders. This factory service is used behind the scenes by $urlRouterProvider to cache compiled UrlMatcher objects,instead of having to re-parse url patterns on every LOCATIOn change. Most users will not need to use $urlMatcherFactory directly,however it Could be useful to craft a UrlMatcher object and pass it as the url to the state config.
    定义url模式和参数占位符的语法。该工厂服务在后台使用$urlRouterProvider来缓存已编译的UrlMatcher对象,当url更改时而不必重新解析位置。大多数用户不需要直接使用$urlMatcherFactory,但是它可以很好地创建一个UrlMatcher对象并将其作为url传递给状态配置。

    Please refer to the comment documentation within the$urlMatcherFactory fileto learn more.
    请参阅$urlMatcherFactory文件中的注释文档以了解更多信息。

    var urlMatcher = $urlMatcherFactory.compile("/home/:id?param1"); $stateProvider.'@H_405_12@myState',{ url: urlMatcher });

    探讨请加微信:

    本图文内容来源于网友网络收集整理提供,作为学习参使用,版权属于原作者。
    @H_772_790@
    @H_696_808@
    @H_616_812@

    猜你在找的Angularjs相关文章

    angular.js实现数据双向通信的原理angular的核心特性有:MVVM、模块化、依赖注入、自动化双向数据绑定、语义标签等。1、AngularJS的scopes对象AngularJS的scopes对象,是一般的javascript对象,可以在他们上面绑定属性和其他对象,也可以添加一些功能,用于观察数据结构上的变化。观察功能都由dirty-checking来实现,并且都在一个digest循环中...
    AngularJS 是一个JavaScript 框架。它可通过 注:建议把脚本放在 元素的底部。这会提高网页加载速度,因为 HTML 加载不受制于脚本加载。AngularJS 扩展了 HTMLAngu
    AngularJS 通过被称为指令的新属性来扩展 HTML。AngularJS 指令AngularJS 指令是扩展的 HTML 属性,带有前缀ng-。ng-app指令初始化一个 AngularJS 应
    AngularJS 使用表达式把数据绑定到 HTML。AngularJS 表达式AngularJS 表达式写在双大括号内:{{ expression }}。AngularJS 表达式把数据绑定到 HT
    $http是 AngularJS 中的一个核心服务,用于读取远程服务器的数据。读取 JSON 文件下是存储在web服务器上的 JSON 文件:{ &quot;records&quot;: [ { &q
    AngularJS 控制器控制AngularJS 应用程序的数据。AngularJS 控制器是常规的JavaScript 对象。AngularJS 控制器AngularJS 应用程序被控制器控制。ng
    keyup适用于文本框的数据输入和同步,以及数据的获取;keydown 与 keypress更适用于通过键盘控制页面功能的实现(如回车事件)
    angularJs模板缓存的清除,包括传统的 HTML标签设置清除缓存,以及angularJs的一些配置清除,和angularJs的路由切换操作清除
    @H_696_808@

    大佬总结

    以上是大佬教程为你收集整理的4、Angular-Ui Router URL路由(完结篇)全部内容,希望文章能够帮你解决4、Angular-Ui Router URL路由(完结篇)所遇到的程序开发问题。

    如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

    本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
    如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。