簡體   English   中英

在創建JSON對象Angular.js之后設置值

[英]Set values after creating a JSON object Angular.js

我在為正在制作的小部件設置一些值時遇到麻煩。 我正在使用Ozone小部件框架,但是在本討論中該部分可以忽略不計。 在這里,我們嘗試設置變量的html(目前僅關注{{user.user}}部分。

<div class="col-lg-12">
    <p>User: {{user.user}}</p>
    <table class="table table-bordered table-hover table-striped">
        <thead>
            <th>#</th>
            <th>Model</th>
            <th>Score</th>
            <th>Date</th>
        </thead>
        <tr data-ng-repeat=" item in records | orderBy : '-score' | limitTo : 10 " ng-click="">
            <td>{{$index+1}}</td>
            <td>{{item.model}}</td>
            <td>{{item.score}}</td>
            <td>{{item.date}}</td>
        </tr>
    </table>
</div>

這是與它一起使用的Angular / owf:

angular.module('myapp', ['cgOwf'])
  .controller('MainCtrl', function($scope, $http, owf) {
    var records;
    $scope.selectPost = '';
    $scope.searchText = ''; 
    console.debug("before IT HERE!!!");
   owf.ready(function(){
       console.debug("MADE IT HERE!!!");
      owf.Eventing.subscribe('user-status', function(sender, msg, channel) {
          console.debug('[%s] - received message %o', channel, msg);
          $scope.user = msg;
      });
   });

    $scope.search = function() {
      //clear the select, go here http://jsonplaceholder.typicode.com/comments
      //and display/filter emails based on the search input
      $scope.selectPost = "";
      $scope.selectedItem = null;
      $http.get('https://api.myjson.com/bins/1jvst').success(function(data2) {
        $scope.records = [];
        data2.forEach(function(r) {
          if (r && r.user && r.user.toLowerCase().indexOf($scope.searchText.toLowerCase()) !== -1) {
            $scope.records.push(r);
          }
        });
      });
    };

  });

我遇到的問題是$scope.user = msg; 在代碼中, msg是一個JSON對象,我確信這是因為它在chrome的js調試器中簽出。 AFAIK就是我要設置對象的方式,這樣我就可以在html中訪問它,盡管顯然不起作用。

owf事件可能不會觸發$digest循環,因此視圖永遠不會更新。 您可以運行$scope.apply()強制$digest

owf.Eventing.subscribe('user-status', function(sender, msg, channel) {
      console.debug('[%s] - received message %o', channel, msg);
      $scope.$apply(function() {
          $scope.user = msg;
      });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM