AngularJS过滤器将日与ID-ng-show进行比较

时间:2015-01-03 07:57:11

标签: javascript angularjs

我使用Angular JS,PHP / MYSQL制作当天的动机。 我在数据库中有31个引号,每月的每一天。 我将如何使用ng-show或ng-hide并将其与今天的&&帖子ID显示匹配的帖子?所以例如今天我们是第3个,所以我的脚本会调用帖子ID 3。

这是我的app.js:

var app = angular.module('myApp',[]);
app.controller('quotesCtrl', function($scope, $http) {
  $http.post('ajax/getQuotes.php').success(function(data){
    $scope.quotes = data;
  });

});

UPDATE!

谢谢彼得,我差不多接近这个工作了!我做了一个var_dump($ arr); enter image description here

任何真正擅长PHP的人,我为粗略的编码道歉。 她是我的getQuotes.php PHP文件:

<?php
include('../includes/config.php');

$query="select id,quote from quotes order by id desc";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

$arr = array();

if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $arr[] = $row;  
    }
}
unset($arr[0]);

$numItems = count($arr);
$i = 0;

foreach($arr as $key => $value ){
    if(++$i === $numItems) {
        echo "$key: '$value[quote]' ";
      }else{
    echo "$key: '$value[quote]', ";
    }
}
echo "<pre>";
var_dump($arr);
//var_dump($arr);

# JSON-encode the response
//$json_response = json_encode($arr);

// # Return the response
//echo $json_response;


?>

我的app.js文件是:

var app = angular.module('app',[]);
app.controller('TimeCtrl', function($scope, $filter, $http) {
  $http.post('ajax/getQuotes.php').success(function(data){
    $scope.quotes = data;
  });
   $scope.dayNumber = $filter('date')(new Date(), 'd');
   console.log($http);
});

我得到的只是:

3 
'

而不是得到:

3
its the third day of the month

***更新2 ****

在app.js中,以下函数错误输出:

$timeout(function() {
          // Parse the JSON
          $scope.quotes = JSON.parse(data);
      })

错误说:

 SyntaxError: Unexpected token o
    at Object.parse (native)

当我将函数更改为stringify时:

$timeout(function() {
          var jsonString = JSON.stringify( data );

          $scope.quotes = jsonString;
          console.log($scope.quotes);
      })

错误消失但输出仍然不正确,就像我执行console.log($ scope.quotes);我明白了:

{"1":{"id":"3","quote":"its the third day of the month"},"2":{"id":"2","quote":"its the second day of the month"},"3":{"id":"1","quote":"its the first day of the month"}}

****更新2 ********

我更新了函数,错误已经消失,正如Peter所建议的那样app.js看起来像这样:

var app = angular.module('app',[]);
app.controller('TimeCtrl', function($scope, $filter, $http, $timeout) {
  $http.post('ajax/getQuotes.php').success(function(data){
    //$scope.quotes = data;

// This triggers a 'digest', to tell the views to update.
      $timeout(function() {
          console.log(data);
        $scope.quotes = data;
      })

  });
   $scope.dayNumber = $filter('date')(new Date(), 'd');
});

然而,输出看起来像json,请看下面的截图: enter image description here

3 
{"id":"1","quote":"its the first day of the month"}

这是上面截图中的特写,应该更容易阅读: enter image description here

1 个答案:

答案 0 :(得分:1)

使用内置日期过滤器的angularJS。这是一个让事情变得简单的工作示例:

http://plnkr.co/edit/X1k3nQdYi9K1zfP2yfR6?p=preview

代码:

<html>

<head>
  <meta charset="UTF-8" />
  <title>require moment</title>
</head>

<body ng-app="app">
  <div ng-controller="TimeCtrl">
    <p>
      {{dayNumber}}
      <br />{{quotes[dayNumber]['quote']}}
    </p>
  </div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular.js"></script>
  <script>
    angular.module("app", [])
      .controller("TimeCtrl", function($scope, $filter) {
        $scope.quotes = {
          1: 'Its the first of the month',
          2: 'Its the second of the month',
          3: 'Its the third of the month',
          4: 'Its the fourth of the month',
          5: 'Its the fifth of the month',
          6: 'Its the sixth of the month'
        }
        $scope.dayNumber = $filter('date')(new Date(), 'd');
      });
  </script>
</body>

</html>

输出是:

4 
Its the fourth of the month

===========

添加PHP代码和AngularJS之后:

你应该回到使用json_encode,不要试图自己做。只记得在javascript中解码它:

/*
foreach($arr as $key => $value ){
    if(++$i === $numItems) {
        echo "$key: '$value[quote]' ";
      }else{
    echo "$key: '$value[quote]', ";
    }
}

echo "<pre>";
var_dump($arr);
*/
//var_dump($arr);

# JSON-encode the response
$json_response = json_encode($arr);

// # Return the response
echo $json_response;

在忘记检索后,不要忘记将JSON从字符串解码为javascript对象:

var app = angular.module('app',[]);
app.controller('TimeCtrl', function($scope, $filter, $http, $timeout) {
  $http.post('ajax/getQuotes.php').success(function(data){
      // This triggers a 'digest', to tell the views to update.
      $timeout(function() {
          // Parse the JSON
          $scope.quotes = JSON.parse(data);
      })
  });
  $scope.dayNumber = $filter('date')(new Date(), 'd');

   // Also, this won't do anything sensible
   console.log($http);
});

最后,如果结果绑定到视图并且您需要查看它更新,则应将更改包装在$ timeout中或调用$ rootScope。$ apply,参见上文。