即使路由有参数,$ stateParams也会变为空值

时间:2015-06-18 11:49:53

标签: angularjs routing angular-ui-router

我正在点击这个网址

  

http://localhost:8001/#/verifyMail?4c4e77a2

那时我做了

  

的console.log($ stateParams);

显示空对象

我的路线是

.state('mailVerification', {
        url: "/verifyMail?id",
          templateUrl: "app/views/common/emailVerification.html",
          controller: 'accountCtrl',
          data: {
              authorizedRoles: [USER_ROLES.all]
          }
      })

为什么我没有获得$ stateParams.id值?请帮帮我。

1 个答案:

答案 0 :(得分:0)

您需要解析数据。

private class BtnNewButtonActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        Card card = B.pop(); //Card one is from Deck B, need to compare to the button

        int actionValue = Integer.parseInt(e.getActionCommand());
        int cardValue = (actionValue - 1) + (card.getSuitNumber() * 13);
        Card match = new Card(cardValue);

        System.out.println("Match " + match + " to " + card);

        if (card.equals(match)) {
            System.out.println("Winner");
        } else {
            System.out.println("Loser");
        }
    }

}

并在控制器内部:

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class CardGame {

    private JFrame frame;
    private JLabel lblA;
    private Deck B = new Deck(); //B have 52 cards, and shuffled  

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CardGame window = new CardGame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public CardGame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(4, 4, 4, 4);
        gbc.ipadx = 15;
        gbc.fill = gbc.HORIZONTAL;

        for (int index = 0; index < 13; index++) {

            System.out.println(index + " - " + (index % 3));

            gbc.gridx = index / 6;
            gbc.gridy = index % 6;

            JButton btn1 = new JButton(Integer.toString(index + 1));
            btn1.addActionListener(new BtnNewButtonActionListener());
            frame.getContentPane().add(btn1, gbc);

        }
    }

    private class BtnNewButtonActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            Card card = B.pop(); //Card one is from Deck B, need to compare to the button

            int actionValue = Integer.parseInt(e.getActionCommand());
            int cardValue = (actionValue - 1) + (card.getSuitNumber() * 13);
            Card match = new Card(cardValue);

            System.out.println("Match " + match + " to " + card);

            if (card.equals(match)) {
                System.out.println("Winner");
            } else {
                System.out.println("Loser");
            }
        }

    }

    public static class Card {

        //Numerical equivalent of the suit and face
        private int suitNum; // valid range is 0 - 3
        private int faceNum; // valid range is 0 - 12
        //For converting between names and numbers
        public static final String[] SUITS = {"Clubs", "Hearts", "Spades", "Diamonds"};
        public static final String[] FACES = {"Ace", "Two", "Three", "Four", "Five", "Six",
            "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

        //Constructor takes combined number and splits it to suit and face numbers
        public Card(int num) {
            if (num > 51) {
                System.out.println("Input number is larger than 51.");
                System.exit(0);
            }
            suitNum = num / 13;
            faceNum = num % 13;
        }

        // Return a calculated value that combines suit and face numbers
        public int getTotalNumber() {
            return (this.suitNum * 13 + this.faceNum);
        }

        public int getFaceNumber() {
            return faceNum;
        }

        public int getSuitNumber() {
            return suitNum;
        }

        public String toString() {
            int num = getTotalNumber();
            String outStr = FACES[num % 13];
            outStr += " of ";
            outStr += SUITS[num / 13];
            return outStr;
        }

        @Override
        public int hashCode() {
            int hash = 7;
            hash = 97 * hash + this.suitNum;
            hash = 97 * hash + this.faceNum;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Card other = (Card) obj;
            if (this.suitNum != other.suitNum) {
                return false;
            }
            if (this.faceNum != other.faceNum) {
                return false;
            }
            return true;
        }

    }

    public class Deck extends Stack<Card> {
        //pop method already in the stack 

        // Create a new shuffled deck
        public Deck() {
            for (int ii = 0; ii < 52; ii++) {
                push(new Card(ii));
            }
            Collections.shuffle(this);
        }

        // For debug purposes
        public void printDeck() {
            for (int ii = 0; ii < 52; ii++) {
                System.out.println(get(ii).toString());
            }
        }
    }

}