如何从列表中获取所选值

时间:2022-12-03 10:14:42

I am new to angularjs and javascript.I am using a contextmenu in the text-angular. So, My code is like -

我对angularjs和javascript不熟悉。我使用的是文本角度的contextmenu。我的代码是-

<div contextmenu="meta.contextmenu" class="dropdown contextmenu ">
            <ul class="dropdown-menu dropdown-content" role="menu" ng-if="showdropdownForProject" id="projectAnnotation">
                <li>
                    <a role="menu" href
                    ng-click="sendInfo()">
                    <span>ABC</span>
                    </a>
                </li>
                <li>
                    <a role="menu" href
                    ng-click="sendInfo()">
                    <span>PQR</span>
                    </a>
                </li>
                <li>
                    <a role="menu" href
                    ng-click="sendInfo()">
                    <span>XYZ</span>
                    </a>
                </li>
</div>

So, Here I am not able to get the selected value in the controller . So, How can I get that value , If I click on the ABC, I need this value so that I can send it to backend. Thanks in advance.

这里,我无法在控制器中获取所选的值。我怎么才能得到这个值,如果我点击ABC,我需要这个值,这样我就可以把它发送到后端。提前谢谢。

2 个解决方案

#1


1  

You should pass value to sendInfo() method.

您应该将值传递给sendInfo()方法。

<li>              
   <a role="menu" href="" ng-click="sendInfo('ABC')">
        <span>ABC</span>
   </a>
</li>

JS

JS

$scope.sendInfo=function(info){
     console.log(info);
} 

#2


1  

You should create array items for it to use ng-repeat.

您应该为它创建要使用ng-repeat的数组项。

Controller:

控制器:

$scope.items = [
  {
    name: 'ABC'
  },
  {
    name: 'BCD'
  },
  {
    name: 'XYZ'
  }
];

$scope.selectedItem = {};

$scope.sendInfo(item) {
  $scope.selectedItem = item;
  console.log($scope.selectedItem.name);
}

Template:

模板:

<div contextmenu="meta.contextmenu" class="dropdown contextmenu ">
  <ul class="dropdown-menu dropdown-content" role="menu" ng-if="showdropdownForProject" id="projectAnnotation">
      <li ng-repeat="item in items track by $index">
          <a role="menu" href
          ng-click="sendInfo(item)">
          <span>{{item.name}}</span>
          </a>
      </li>
    </ul>
</div>

#1


1  

You should pass value to sendInfo() method.

您应该将值传递给sendInfo()方法。

<li>              
   <a role="menu" href="" ng-click="sendInfo('ABC')">
        <span>ABC</span>
   </a>
</li>

JS

JS

$scope.sendInfo=function(info){
     console.log(info);
} 

#2


1  

You should create array items for it to use ng-repeat.

您应该为它创建要使用ng-repeat的数组项。

Controller:

控制器:

$scope.items = [
  {
    name: 'ABC'
  },
  {
    name: 'BCD'
  },
  {
    name: 'XYZ'
  }
];

$scope.selectedItem = {};

$scope.sendInfo(item) {
  $scope.selectedItem = item;
  console.log($scope.selectedItem.name);
}

Template:

模板:

<div contextmenu="meta.contextmenu" class="dropdown contextmenu ">
  <ul class="dropdown-menu dropdown-content" role="menu" ng-if="showdropdownForProject" id="projectAnnotation">
      <li ng-repeat="item in items track by $index">
          <a role="menu" href
          ng-click="sendInfo(item)">
          <span>{{item.name}}</span>
          </a>
      </li>
    </ul>
</div>