如何通过AngularJS路由在url中传递选定的id并在console.log中获取数据?

时间:2017-11-21 04:58:12

标签: angularjs url-routing

我在Url中传递选定的车牌ID并尝试在console.log中获取数据不知道我在哪里丢失可能是我在stackoverflow中检查太多解决方案时感到困惑 - 这是我的代码:

// Controller.js

     $scope.chooseACar = function(selectCarId)
            { 
               $scope.selectCarId = $stateParams.selectCarId;
                console.log($scope.selectCarId);
            }

// App.js

    .state('main.step3', 
    {
        url: '/step2/:selectCarId',
        templateUrl: 'pages/step3.html'
    });

// services.js

get: function(selectCarId) {
    for (var i = 0; i < carListData.length; i++) {
      if (carListData[i].id === parseInt(selectCarId)) {
        console.log(carListData[i]);
        return carListData[i];
      }
    }
    return null;
  }

// HTML代码

<a type="submit" href="#/main/step2/{{selectCarList.id}}" class="button button-yellow button-full submitBtn" style="color:#000; display:block">CHOOSE A CAR</a>

2 个答案:

答案 0 :(得分:2)

在App.js中(如果你在url中获取ID,则无需修改。)

.state('main.step2', {
    name: 'slectcarID'
    url: '/step2/:selectCarId',
    templateUrl: 'pages/step3.html'
});

并获取控制器中的数据使用$ stateParams

console.log($stateParams.selectCarId);

$scope.selectCarId = $stateParams.selectCarId;
console.log($scope.selectCarId);

尝试仅使用ng-click或ui-sref

答案 1 :(得分:0)

尝试使用 ui-sref 代替href

HTML

enter code here
   flashButton = findViewById(R.id.flash_button);
            mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
            try {
                mCameraId = mCameraManager.getCameraIdList()[0];
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }

            flashButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        if (!isTorchOn) {
                            turnOnFlashLight();

                        } else {
                            turnOffFlashLight();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            scrollValue = findViewById(R.id.value);
            mWheel = findViewById(R.id.wheel);
            mWheel.setOnScrollListener(new Wheel.OnScrollListener() {


                @Override
                public void onScrollStarted(Wheel view, float value, int roundValue) {
                    frequency = Math.abs(roundValue);
                }

                @Override
                public void onScrollFinished(Wheel view, float value, int roundValue) {
                    scrollValue.setText("" + Math.abs(roundValue));
                    frequency = strobeFrequency(Math.abs(roundValue));
                    if(isTorchOn && frequency > 0) {
                       thread = new Thread(stroboRunner);
                         thread = null;
                        strobe(frequency);
                    }
                    else if(isTorchOn) {
                        strobe(0);
                       // thread.stop();
                       // thread=null;
                    }



                }

                @Override
                public void onScroll(Wheel view, float value, int roundValue) {
                    scrollValue.setText("" + Math.abs(roundValue));
                    frequency = strobeFrequency(Math.abs(roundValue));
                    if(isTorchOn && frequency > 0) {
                       // thread = null;
                        strobe(frequency);
                    }

                }
            });

        }


        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        private void turnOnFlashLight() {
    /*
            if(frequency > 0){
                scrollValue.setText("0.0");
            }
    */
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mCameraManager.setTorchMode(mCameraId, true);
                } else {
                    try {
                        camParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        cam.setParameters(camParams);
                        cam.startPreview();
                    } catch (RuntimeException e) {
                        Toast.makeText(this, "Camera Permission is not granted", Toast.LENGTH_SHORT).show();
                    }
                }
                flashButton.setImageResource(R.drawable.power_on);
                isTorchOn = true;
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }


        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        private void turnOffFlashLight() {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if(thread != null){
                        stroboRunner.stopRunning = true;
                        thread = null;
                    }else {
                        mCameraManager.setTorchMode(mCameraId, false);
                    }
                } else {
                    try {
                        if(thread != null){
                            stroboRunner.stopRunning = true;
                            thread = null;
                        }else {
                            camParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                            cam.setParameters(camParams);
                            cam.stopPreview();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(getBaseContext(), "Exception throws in turning off flashlight.", Toast.LENGTH_SHORT).show();
                    }
                }
                flashButton.setImageResource(R.drawable.power_off);
                isTorchOn = false;
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }

        private int strobeFrequency(int roundValue) {
            int freq = 0;
            switch (roundValue) {
                case 1:
                    freq = Math.abs(50);
                    break;
                case 2:
                    freq = Math.abs(150);
                    break;
                case 3:
                    freq = Math.abs(300);
                    break;
                case 4:
                    freq = Math.abs(600);
                    break;
                case 5:
                    freq = Math.abs(900);
                    break;
                case 6:
                    freq = Math.abs(1200);
                    break;
                case 7:
                    freq = Math.abs(1500);
                    break;
                case 8:
                    freq = Math.abs(1800);
                    break;
                case 9:
                    freq = Math.abs(2000);
                    break;
                case 10:
                    freq = Math.abs(2300);
                    break;
            }
            return freq;
        }


        @Override
        protected void onResume() {
            super.onResume();

            try {
                cam = Camera.open();
                camParams = cam.getParameters();
                cam.startPreview();
                hasCam = true;
            } catch (Exception e) {
                // TODO: handle exception
            }

            compass.start();
        }

        @Override
        protected void onPause() {
            super.onPause();
            compass.stop();
        }

        @Override
        protected void onStop() {
            super.onStop();
            Log.d(TAG, "stop compass");
            compass.stop();

        }


       private void strobe(int freq) {
            if(thread == null){
                stroboRunner = new StroboRunner();
                stroboRunner.freq = freq;
                thread = new Thread(stroboRunner);
                thread.start();
            } else if(freq == 0 && thread!=null) {
                stroboRunner.stopRunning = true;
                thread = null;
               // thread.stop();
            }
        }

        private class StroboRunner implements Runnable {
            int freq;
            boolean stopRunning = false;

            @Override
            public void run() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    try {
                        while (!stopRunning) {
                            mCameraManager.setTorchMode(mCameraId, true);
                            Thread.sleep(2600 - freq);
                           System.out.println(""+(2600-freq));
                            // Toast.makeText(MainActivity.this, ""+(2600-freq), Toast.LENGTH_SHORT).show();
                            mCameraManager.setTorchMode(mCameraId, false);
                            Thread.sleep(freq);
                        }
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                  // thread=null;
                } else {
                    Camera.Parameters paramsOn = cam.getParameters();
                    Camera.Parameters paramsOff = camParams;
                    paramsOn.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    paramsOff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    try {
                        while (!stopRunning) {
                            cam.setParameters(paramsOn);
                            cam.startPreview();
                            Thread.sleep(2600 - freq);
                            cam.setParameters(paramsOff);
                            cam.startPreview();
                            Thread.sleep(freq);
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        }

App.js

<a type="submit" ui-sref="main.step3({carId : selectCarList.id})"
   class="button button-yellow button-full submitBtn" 
   style="color:#000; display:block">CHOOSE A CAR
</a>

控制器

.state('main.step3', 
{
    url: '/step2/:selectCarId',
    templateUrl: 'pages/step3.html',
    controller: `Your Controller Name`,
    params: { carId: null, }
});
相关问题