从ng-repeat上点击访问li数据

时间:2015-07-31 01:09:58

标签: javascript angularjs

如何从ng-repeat中记录/访问对象数据?我希望能够在单击其 li 元素时访问每个特定的firebase对象。我已经能够记录整个对象,但我需要仅针对特定的 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身边,这让我疯狂。

1 个答案:

答案 0 :(得分:1)

HTML:

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

控制器:

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

只需阅读ng-repeat的角度文档,您就会明白它是如何工作的。