当我从服务器加载我的json数据时,我需要在json数据上只有其他视图属性。但这不可能。
然后我考虑从像以下工厂创建angularjs模型:
'use strict';
angular.module('TGB').factory('TestViewModel', function () {
function TestViewModel(test) {
this.id = test.id;
this.schoolclassCode = test.schoolclassCode;
this.testType = test.type;
this.creationDate = test.date;
this.number = test.number;
this.isExpanded = false;
}
return (TestViewModel);
});
您在哪里以角度创建这些视图模型?
At the moment I have this method in my Controller , call it there and assign the result to $scope.testViewModels = toTestListViewModel(tests)
function toTestListViewModel(tests)
{
var testListViewModel = [];
for (var i = 0; i < tests.length; i++) {
var testViewModel = new TestViewModel(tests[i]);
testListViewModel.push(testViewModel);
}
return testListViewModel;
}
答案 0 :(得分:0)
控制器是创建视图模型的合适位置。可以使用$ scope访问。
.controller('sampleCtrl', function ($scope, testViewModel) {
$scope.vM = testViewModel;
}