从其他页面过滤角度

时间:2017-03-21 15:53:14

标签: javascript angularjs

我通过过滤器进行角度搜索。

  <input type="text" name="search_job" ng-model="search" />

<article ng-repeat="post in posts  | filter : filterObject |  filter : search ">
<h3><a href="#/post/{{ posts.indexOf(post) }}">{{ post.title }}</a></h3>
<cite>by {{post.author}} on {{post.created_at | date}}</cite>
<p>{{post.body | limitTo:250}}..</p>
<a href="#/post/{{ posts.indexOf(post) }}">Read more &rarr;</a>

我需要从其他页面中的其他搜索进行过滤。所以,我通过GET发送表单并用PHP格式化它。我在窗口Javascript变量

中转换php变量
window.myJsvar = <?=$_GET["word"]?>;

而且,在app.js中我把它放在$ scope变量

app.controller('PostController', ['$scope', '$http', function($scope, $http) {
    $http.get('json/jobs.json').success(function(data) {
        $scope.posts = data;
        $scope.filterObject = window.myJsvar;
    });
}])

但它不起作用。我该怎么做?

1 个答案:

答案 0 :(得分:0)

我正在研究服务,但是,我试图这样做,但我不知道如何在服务中插入$ rootScope。

 <form name="search-form">
<input type="text" placeholder="Enter key word" name="word" ng-model="formData.word" />
<br/><br/><input type="submit" value="Search" class="btn" ng-click="submitForm(formData)"/>
</form>

app.js

     (function(){
  var app = angular.module('App',[
    'ngRoute',
    'ui.router',
    'multipleSelect',
    'ngSanitize',
    ]).config(['$routeProvider', function($routeProvider){
  $routeProvider.when('/', {
    templateUrl: 'web/jobs/post.php',
    controller: 'PostController'
  })
}]);

app.service('serveData', [function () 
{
  return {
    qty : $rootScope.filterObject
  };
}])

  app.controller('MainCtrl', function ($scope,$rootScope,$window) {
    $scope.formData = {};
    $scope.submitForm = function (formData) {
    $rootScope.filterObject = formData.word;

    //alert('Form submitted with' + JSON.stringify($rootScope.filterObject));
    $window.location.href = "jobs";
  };
  });

      app.controller('PostController', ['$scope', '$http','$rootScope','serveData', function($scope, $http, $rootScope, serveData){
    $http.get('json/jobs.json').success(function(data){
      $scope.posts = data;
      $scope.obj = serveData;
    });

  }])

post.php中

<article ng-repeat="post in posts  | filter : obj.qty |  filter : search ">
    <h3><a href="#/post/{{ posts.indexOf(post) }}">{{ post.title }}</a></h3>
    <cite>by {{post.author}} on {{post.created_at | date}}</cite>
    <p>{{post.body | limitTo:250}}..</p>
    <a href="#/post/{{ posts.indexOf(post) }}">Read more &rarr;</a>
</article>