通过点击ng-repeat访问li数据

Accessing li data on click from ng-repeat

本文关键字:访问 li 数据 ng-repeat      更新时间:2023-09-26

如何从ng-repeat记录/访问对象数据?我希望能够访问每个特定的firebase对象,当我点击它的li元素。我已经能够记录整个对象,但是我只需要针对特定的li的数据?

pomodoro_timer.controller('timer', ['$scope','$interval','$firebaseArray', function ($scope, $interval, $firebaseArray) {
      var ref = new Firebase("https://pomodoro-timer7710.firebaseIO.com/");
      $scope.working = $firebaseArray(ref)
      $scope.addWork = function() {
        if ($scope.newWork == null) {
          WorkInputField.placeholder = "Please enter a job"
        }
        $scope.working.$add({
          name: $scope.newWork,
          startedAt: Date.now(),
          completed: false
        });
        $scope.newWork = "";
      }
        $scope.showTime = function() {
          ref.on("value", function(snapshot) {
            console.log(snapshot.val());
          }, function (errorObject) {
            console.log("The read failed: " + errorObject.code);
          });
        }
<标题> HTML
      <div id="workInfo">
        <div id="enterWork">
          <form ng-submit="addWork()">
            <input id="WorkInputField" type="text" placeholder="Job to complete" ng-model="newWork"></input>
          </form>
          <h3>Your Work:</h3>
          <ul>
            <li class="workList" ng-repeat="work in working" ng-click="showTime()">          
              {{ work.name }}
              <a href="#" ng-click="working.$remove(work)"><span class="removeWork">Done</span></a>
            </li>
          </ul>
        </div>
      </div>

我希望这是足够的代码,它的所有相关的问题。当我点击每个单独的li时,我想查看它的键/值对。谢谢,我正试着理解Angular,这个东西快把我逼疯了。

HTML:

 <li class="workList" ng-repeat="work in working" ng-click="showTime(work)">   ...</li>

控制器:

$scope.showTime = function(work) { console.log(work);  }

只要阅读ng-repeat的angular文档,你就会明白它是如何工作的。