Javascript 使用其他视图逻辑属性丰富AngularJSON数据

Javascript 使用其他视图逻辑属性丰富AngularJSON数据,javascript,angularjs,json,Javascript,Angularjs,Json,当我从服务器加载json数据时,我需要在json数据上有额外的仅查看属性。但这是不可能的 然后我考虑从以下工厂创建angularjs模型: 'use strict'; angular.module('TGB').factory('TestViewModel', function () { function TestViewModel(test) { this.id = test.id; this.schoolclassCode = test.schoo

当我从服务器加载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;
    }
现在我的控制器中有了这个方法,在那里调用它并将结果分配给$scope.testViewModels=toTestListViewModel(tests)
函数toTestListViewModel(测试)
{
var testListViewModel=[];
对于(变量i=0;i
控制器是创建“视图模型”的合适位置,您可以使用$scope访问该模型

.controller('sampleCtrl', function ($scope, testViewModel) {
   $scope.vM = testViewModel;
}

我想你误解我了。我想/必须自己通过测试json实体来更新TestViewModel实例。我最好在哪里更新viewmodel?我用更多信息更新了我的问题!json从哪里来?Http响应?那么为什么不在控制器中使用$Http服务,或者在工厂中添加一个方法来调用后端并重新填充数据呢?我使用具有resolve属性的ui路由器。我不调用控制器中的$htp服务。我有客户端服务包装$http调用。我不熟悉ui路由器框架,所以无法帮助您。很抱歉