得到错误!!!同时使用angularjs和angular fomrly读取json文件

时间:2016-06-03 07:54:20

标签: angularjs angular-formly

尝试在角度js中读取JSON文件并将其传递给角度以获取html输入元素,使用angular.factory读取外部json文件并将数据传递给控制器​​,在此处将json数据分配给字段,在调试时我已经检查了所有数据正确地从JSON文件中获取但仍然没有输出并且还得到控制台错误,我已粘贴error link

我特此粘贴我的代码和JSON file如果有任何问题,请纠正我

<div ng-app="formlyExample" ng-controller="MainCtrl as vm">
<div ng-if="vm.loadingData.$$state.status === 0" style="margin:20px 0;font-            size:2em">
<strong>Loading...</strong>
</div>
<div ng-if="vm.loadingData.$$status.state !== 0">
<form ng-submit="vm.onSubmit()" novalidate>
<formly-form model="vm.model" fields="vm.fields" form="vm.form">
<button type="submit" class="btn btn-primary submit-button">Submit</button
</formly-form>
</form>
</div>
</div>

var app = angular.module('formlyExample', ['formly', 'formlyBootstrap']);
app.run(function(formlyConfig) {
    formlyConfig.setType({
        name: 'ipAddress',
        extends: 'input',
        defaultOptions: {
            validators: {
                ipAddress: {
                    expression: function(viewValue, modelValue) {
                        var value = modelValue || viewValue;
                        return /(\d{1,3}\.){3}\d{1,3}/.test(value);
                    },
                    message: '$viewValue + " is not a valid IP Address"'
                }
            }
        }
    });
});
app.controller('MainCtrl', function MainCtrl(formlyVersion, User) {
    var vm = this;
    vm.onSubmit = onSubmit;
    vm.loadingData = User.getData().then(function(data) {
        vm.model = data[0];
        vm.fields = data[1];
    });

    function onSubmit() {
        alert(JSON.stringify(vm.model), null, 2);
    }
});
app.factory('User', function($http, $q) {
    return {
        getData: function() {
            return $http.get('Content/test_field.json')
                .then(function(response) {
                    if (typeof response.data === 'object') {
                        return response.data;
                    } else {
                        return $q.reject(response.data);
                    }
                }, function(response) {
                    return $q.reject(response.data);
                });
        }
    };
});

0 个答案:

没有答案