2015-07-12 70 views
0

目前,我正在学习新的MVC 6,用简单的动作完全stucked - 在项目选择change.The期望的行为表中的数据更新是加载属于所选问题块ASP.NET MVC 6角JS表更新

问题

我有angularJS厂:

(function() { 
 
    'use strict'; 
 

 
    angular 
 
     .module('questionBlockApp') 
 
     .factory('questionBlockService', questionBlockService); 
 

 
    var questionBlockService = angular.module('questionBlockService', ['ngResource']); 
 

 
    questionBlockService.factory('Blocks', ['$resource', 
 
    function ($resource) { 
 
     return $resource('/api/blocks/', {}, { 
 
      query: { method: 'GET', params: {}, isArray: true } 
 
     }); 
 
    }]); 
 

 
    questionBlockService.factory('Questions', ['$resource', 
 
    function ($resource) { 
 
     return $resource('/api/blocks/:blockId', {blockId : '@blockId'}, { 
 
      query: { method: 'GET', params: {}, isArray: true } 
 
     }); 
 
    }]); 
 

 
})();

控制器,它具有刷新FUNC(loadQuestions)选择改变函数内部内置:

(function() { 
 
    'use strict'; 
 

 
    angular 
 
     .module('questionBlockApp') 
 
     .controller('questionBlockController', questionBlockController); 
 
     //.controller('questionController', questionController); 
 

 
    questionBlockController.$inject = ['$scope', 'Blocks', 'Questions']; 
 
    //questionController.$inject = ['$scope', 'Questions']; 
 

 
    function questionBlockController($scope, Blocks, Questions) { 
 
     $scope.selectedBlock = 2; 
 

 
     if ($scope.Blocks == undefined | $scope.Blocks == null) { 
 
      $scope.Blocks = Blocks.query(); 
 
     } 
 

 
     $scope.setSelected = function (blockId) { 
 
      $scope.selectedBlock = blockId; 
 
      $scope.loadQuestions(); 
 
     } 
 
     
 
     $scope.loadQuestions = function() { 
 
      $scope.data = Questions.query({ blockId: $scope.selectedBlock }); 
 
      $scope.data.$promise.then(function (data) { 
 
       $scope.Questions = data; 
 
      }); 
 
     }; 
 

 
     $scope.loadQuestions(); 
 
    } 
 

 
})();

和看法:

观从的setSelected被称为:

<table class="table table-striped table-condensed" ng-cloak ng-controller="questionBlockController"> 
 
       <thead> 
 
        ... 
 
       </thead> 
 
       <tbody> 
 
        <tr ng-repeat="block in Blocks" ng-click="setSelected(block.Id)" ng-class="{'selected': block.Id == selectedBlock}"> 
 
         <td>{{block.Id}}</td> 
 
         <td>{{block.Name}}</td> 
 
         <td>{{block.Created}}</td> 
 
        </tr> 
 
       </tbody> 
 
      </table>

<table id="test" ng-controller="questionBlockController"> 
 
        <thead> 
 
         <tr> 
 
          ... 
 
         </tr> 
 
        </thead> 
 
        <tbody> 
 
         <tr ng-repeat="question in Questions"> 
 
          <td>{{question.Id}}</td> 
 
          <td>{{question.Text}}</td> 
 
          <td>{{question.TimeLimit}}</td> 
 
          <td>{{question.Updated}}</td> 
 
         </tr> 
 
        </tbody> 
 
       </table>

当我点击在QuestionBlock表不同的项目,$ scope.Questions正确更新,但该表不能反映更改,如果没有绑定存在的。

+0

所以这总是显示与'selectedBlock = 2' rt ??相关的数据或者你在绑定'ng-click'指令来调用'setSelected'函数? –

+0

Y,sry,这是ng-click在另一个表格中,它提出了问题块 – George

回答

0

好吧,我只是有点损坏。 定义了两个questionBlockController控制器,导致不同作用域的初始化=>两个$scope.Questions对象存在=>刷新发生在块范围内,这是不理想的行为。