2013-04-12 24 views
2

我正在开发一个页面的应用程序,并且我想要调用服务器来查询特定价格的物品(例如www.myapp.com/products?price = 1200)。我能够执行请求并从端点获取JSON,但该视图不更新并显示数据。任何想法为什么我的观点不更新?AngularJS - 发送和接收数据但不更新视图

我的第一个想法是,问题是在$ routeProvider,但我不知道我需要如何改变它。

谢谢!

我的角度模块

angular.module('myApp', []) 
    .config(['$routeProvider', function($routeProvider) { 

    $routeProvider. 
    when('/products', 
     { templateUrl: 'partials/productSearchTemplate.html', 
     controller: ProductControl 
     }); 
}]); 

我控制器

function ProductControl($scope, $http) { 
    $scope.sendRequest = function() { 
    $http({ 
     url: '/products', 
     method: "GET", 
     query: {price: $scope.price} 
    }).success(function(data) { 
     $scope.products = data;  
    }); 
    } 
} 

我的观点

<div class="products"> 
    <div ng-repeat="product in products" class='product-panel'> 
     <div class='front'>     
      <span class='product-title'>{{product.title}}</span><br/> 
      <span class='product-manufacturer'> 
       {{product.manufacturer}} 
      </span> 
     </div> 
     <div class='back'> 
      <span class='product-price'> 
       {{product.price}} 
      </span><br/> 
      <a href="{{product.url}}">Details</a> 
     </div> 
    </div> 
</div> 
+1

从哪一点你要sendRequest –

+0

我在我的index.html页面有一个按钮

+0

如果你的按钮不在productSearchTemplate中,因此我怀疑它没有权限访问ProductControl的范围,因此无法调用sendRequest。 – mfelix

回答

0

我怀疑你的 '产品' 端点不返回数组,而是对象。以下从服务器的JSON你的期望:

[ 
    {"title":"title","price":1,"manufacturer":"Someone"} 
] 

但最有可能的服务器返回的对象,而不是数组:

{ 
    "products":[{"title":"title","price":1,"manufacturer":"Someone"}], 
    "totalCount": 1 
} 

所以只是通过萤火虫或Chrome浏览器开发工具,什么是从服务器返回检查,并执行必要的控制器转换。