呼叫控制器功能只有一次,两个div与同一个控制器

时间:2014-09-20 05:33:24

标签: javascript angularjs asp.net-mvc-3

我正在研究angularjs。我有两个div共享相同的控制器。我的控制器代码是。

var app=angular.module('categoryPageApp', []);
app.controller('NgProducts', function($scope,UpdateService) {

   $scope.orderby=5;
   $scope.pageNumber=1;
   $scope.loadProducts=function($scope){
             //here goes ajax call
     };

 });

我的两个具有相同控制器的div是

<div ng-app="categoryPageApp">
<div id="cat-products" ng-controller="NgProducts">
</div>
<div id="brand-filter" ng-controller="NgProducts">
</div>
</div>

正如我们所看到的,现在两次声明所有变量,并且还将调用更新fucntion。 我不希望这发生,因为单次http调用将完成工作。有没有解决方法。

此外,我想使用角度js为我的产品构建寻呼机。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

这样做:

<div ng-app="categoryPageApp" ng-controller="NgProducts">
    <div id="cat-products"></div>
    <div id="brand-filter"></div>
</div>

答案 1 :(得分:1)

您可以在加载产品后尝试存储一些标记,如下所示:

app.controller('NgProducts', function($scope,UpdateService) {
   if (!$scope.productsAreLoaded)
     $scope.orderby=5;
     $scope.pageNumber=1;
     $scope.loadProducts=function($scope){
        //here goes ajax call
        $scope.productsAreLoaded = true;
     };

});
相关问题