用额外的视图逻辑属性丰富angularjs-json数据

Enrich angularjs json data with additional view logic properties

本文关键字:属性 angularjs-json 数据 视图      更新时间:2023-09-26

当我从服务器加载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访问的"视图模型"的合适位置。

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