从视图到控制器检索变量的值

时间:2018-04-20 08:39:58

标签: javascript html angularjs

我正在尝试从视图中检索一个值到控制器。该值是在运行时生成的。

<ng-option ng-repeat="w in details track by $index">
    <div class="program-grid-row table-row" >
      <div>
          <a class= ng-click="setWork('hello')" 
                  href="#/helloworls/{{w[5]}}">{{w[5]}}</a>
      </div>
    </div>
</ng-option>

我想在我的控制器中检索w [5]的值 controller.js

$http.get(Url +$scope.w[5]).then(function(response){
  $rootScope.details= response.data;
});

不确定如何在控制器中检索w [5]的值。请帮助。

2 个答案:

答案 0 :(得分:1)

您可以尝试获取w[5]的最后一个值的$scope.details,但我认为您的问题本身并不正确:

<ng-option ng-repeat="w in details track by $index">
<div class="program-grid-row table-row" >
  <div>
      <a class= ng-click="setWork('hello')" 
              href="#/helloworls/{{w[5]}}">{{myData = w[5]}}</a>
  </div>
</div>
</ng-option>
控制器中的

$http.get(Url +$scope.myData).then(function(response){
   $rootScope.details= response.data;
});

或者

<ng-option ng-repeat="w in details track by $index">
<div class="program-grid-row table-row" >
  <div>
      <a class= ng-click="setWork(w[5])" 
              href="#/helloworls/{{w[5]}}">{{w[5]}}</a>
  </div>
</div>
</ng-option>
控制器中的

$scope.setWork = function(data){
   $http.get(Url +data).then(function(response){
      $rootScope.details= response.data;
   });
 }

答案 1 :(得分:0)

您可以将w[5]作为ng-click函数中的参数传递,并在控制器中获取该点击的值:

查看

<ng-option ng-repeat="w in details track by $index">
      <div class="program-grid-row table-row" >
      <div>
      <a class= ng-click="setWork(w[5])"
    href="#/helloworls/{{w[5]}}">{{w[5]}}</a>
    </div>
    </div>
</ng-option>

<强>控制器

$scope.setWork = function(param){
  //param has `w[5]` value which is clicked
  $http.get(Url + param).then(function(response){
    $rootScope.details= response.data;
  });
}