Simple use of Angularjs ui-sref

Source: Internet
Author: User
Specific usage:
<a ui-sref= "man" > Men </a>

This is a very simple ui-sref use, when JavaScript regenerates a page, it looks for the state in $state named "Man", reads the URL of the state, and then generates href= "url" in the A tag.

The results are as follows: <a ui-sref= "man" href= "#/man.html" > Men </a> But if you are creating a navigation controller that has an array of navigation item:

    $scope. Items = [
      {state: ' Man ', Statepage: "man.html"},
      {state: "Womanme", Statepage: "woman.html"}
    ]


Then use repeat in HTML:
    <li repeat= "Item in Items" >
    <a ui-sref= "{{item.statepage}}" ><{{item.state}}</a>
    </ Li>


Ui-sref does not support dynamic binding, such code will be an error. You can only use the state name in the Sref, with a few parameters.
In this case, you can only give up sref and use back the href binding, and you could use $STATE.HREF to read the state URL.

The following is a brief introduction to the transfer of UI-SREF parameters

The page is written as follows

<a ui-sref= "man ({Id:1,name:2})" > button </a>

Routing inside Configuration:

$stateProvider. State (' man ', {
    URL: '/man.html?id&name '),         //parameter must first be declared here
    Templateurl: '. /man.html ',
})

When you click on the connection, the browser's address becomes:/man.html/id=1&name=2
Or you can do it.

$stateProvider. State (' man ', {
    URL: '/man.html ',         
    templateurl: '. /man.html ',
    params: {' id ': null, ' name ': null},//parameter declared here

})

And then in the corresponding controller through the $stateparams value: $stateParams. ID, $stateParams. Name


In fact Ui-sref and $state.go is essentially a thing that can look at Ui-sref source

Element.bind ("click", Function (e) {
    var button = E.which | | E.button;
    if (!) ( button > 1 | | E.ctrlkey | | E.metakey | | E.shiftkey | | Element.attr (' target ')) {

      var transition = $timeout (function () {
        //We call $state. Go inside of ui-sref
  
    $state. Go (ref.state, params, options);
      
  





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.