将范围变量与http服务中的数据相关联

时间:2019-08-01 06:20:17

标签: angularjs angularjs-directive angular-ui-router angular-material

app.controller('contentixaeng', function ($scope, $http)  {

    $scope.subject = function(){
        $scope.code=101;
    };

  $http({
    method: "POST",
    data:{
        'subj_code':$scope.code, 'action':'singledata'
    },
    url: "pages/Entries/connectixa.php"
  }).then(function mySuccess(response) {
    $scope.users = response.data;
  }, function myError(response) {
    $scope.error = response.data;
  });
});

我试图将$ scope.code的值传递给HTTP服务中的数据。它无法正常工作,并且没有数据值显示为输出。相反,我收到错误“ ng-repeat dupes”。

通过此行调用函数主体

<li class="nav-item" ng-contoller="contentixaeng"><a class="nav-link" href="#" ui-sref="ixaeng" ng-click="subject()" >English</a></li>

如果我更改如下所示的代码,那么它将起作用

app.controller('contentixaeng', function ($scope, $http)  {

    $scope.subject = function(){
        $scope.code=101;
    };

  $http({
    method: "POST",
    data:{
        'subj_code':101, 'action':'singledata'
    },
    url: "pages/Entries/connectixa.php"
  }).then(function mySuccess(response) {
    $scope.users = response.data;
  }, function myError(response) {
    $scope.error = response.data;
  });
});

我希望基于on-click事件将不同的数据传递到数据库搜索。

2 个答案:

答案 0 :(得分:2)

在创建控制器后立即发送http请求。此时//web.php of laravel routes <?php Route::get('/{any}', function(){ return view('index');//pointing to the index file of the frontend })->where('any', '.*'); ?> 尚未设置。

尝试这样的事情:

$scope.code

像这样,仅在显式调用app.controller('contentixaeng', function ($scope, $http) { $scope.subject = function(){ $scope.code=101; callBackend(); }; function callBackend() { $http({ method: "POST", data:{ 'subj_code':$scope.code, 'action':'singledata' }, url: "pages/Entries/connectixa.php" }).then(function mySuccess(response) { $scope.users = response.data; }, function myError(response) { $scope.error = response.data; }); }); } 方法时发送http请求。

答案 1 :(得分:1)

如果您收到ng-repeat dupes错误,这意味着您在$scope.users中有重复的条目,请尝试对其进行调试,然后查看发生了什么。另外,您可以使用track by选项,如下所示:

ng-repeat="user in users track by $index"

即使您在user变量中有重复的条目,也可以确保将每个$users都视为唯一实体。

另一件事是,这段代码在哪里运行?在您提供的代码中,我看不到任何地方

$scope.subject = function(){
    $scope.code=101;
};