繁体   English   中英

ASP.NET MVC 6 Angular JS表更新

[英]ASP.NET MVC 6 Angular JS Table Update

我目前正在学习新的MVC 6,并通过简单的操作完全卡住了-在项目选择更改时更新表数据。所需的行为是加载属于所选问题块的问题

我有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 } }); }]); })(); 

控制器,具有在选择更改功能内部内置的刷新功能(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已正确更新,但该表未反映更改,就好像没有绑定一样。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM