Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 模型Id已传递到Angular Ctrl_Asp.net Mvc_Angularjs_Razor - Fatal编程技术网

Asp.net mvc 模型Id已传递到Angular Ctrl

Asp.net mvc 模型Id已传递到Angular Ctrl,asp.net-mvc,angularjs,razor,Asp.net Mvc,Angularjs,Razor,我有一个使用下面控制器的MVV视图,我需要以某种方式将产品Id,即Model.Id传递给控制器。 我已经尝试过$scope.init,但在我进行第一次Ajax调用时,它显示为null,我怀疑这个Ajax get是在启动init并设置产品id之前启动的,因此Ajax失败,因为调用时productId为null。我是新来的,所以如果这是一个小学生的错误,我道歉 控制器和HTML如下所示 angular.module('askQuestions', []) .controller('questio

我有一个使用下面控制器的MVV视图,我需要以某种方式将产品Id,即Model.Id传递给控制器。 我已经尝试过$scope.init,但在我进行第一次Ajax调用时,它显示为null,我怀疑这个Ajax get是在启动init并设置产品id之前启动的,因此Ajax失败,因为调用时productId为null。我是新来的,所以如果这是一个小学生的错误,我道歉

控制器和HTML如下所示

 angular.module('askQuestions', [])
 .controller('questionController', function ($scope, $http) {
  $scope.loading = true;
  $scope.addMode = false;
  $scope.replyMode = false;
  $scope.parentClicked = 0;

  $scope.init = function (productId) {
      //This function is sort of private constructor for controller
      $scope.productId = productId;
      $scope.getUrl = '/Api/GetProductQuestions/' + $scope.productId;

  };


  //Used to display the data
  //$http.get('/Api/GetAllManufacturers?apiToken=6a5ce02e-0506-0a41-2f50-37327080662f').success(function (data) {
  $http.get($scope.getUrl).success(function (data) {


      $scope.questions = data;
      $scope.loading = false;

  })
  .error(function () {
      $scope.error = "An Error has occured while loading questions!";
      $scope.loading = false;
     // alert($scope.getUrl);
  });
}))


您的
$http.get
在控制器的实例化中进行评估。实例化在init之前进行,因此已经进行了ajax调用。您可以通过将
$http.get
也包装到函数中来轻松解决此问题:

$scope.init = function (productId) {
    //This function is sort of private constructor for controller
    $scope.productId = productId;
    $scope.getUrl = '/Api/GetProductQuestions/' + $scope.productId;
    getData();
};

var getData = function() {
    $http.get($scope.getUrl)
        .success(function (data) {
            // success
            $scope.questions = data;
        })
        .error(function () {
            // error
            $scope.error = "An Error has occured while loading questions!";
        })
        .finally(function () {
            // always
            $scope.loading = false;
        });
}
$scope.init = function (productId) {
    //This function is sort of private constructor for controller
    $scope.productId = productId;
    $scope.getUrl = '/Api/GetProductQuestions/' + $scope.productId;
    getData();
};

var getData = function() {
    $http.get($scope.getUrl)
        .success(function (data) {
            // success
            $scope.questions = data;
        })
        .error(function () {
            // error
            $scope.error = "An Error has occured while loading questions!";
        })
        .finally(function () {
            // always
            $scope.loading = false;
        });
}