1

Am trying to read a JSON file in angular js and pass it to angular formly for getting the html input elements, am reading a external json file using angular.factory and passing the data to the controller where am assigning the json data to the fields, while debugging I have checked all the datas are getting from the JSON file correctly but still am getting no output and also am getting console error, I have pasted the error link.

I hereby paste my code and JSON file please Correct me if any thing is wrong

<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);
                });
        }
    };
});
2
  • please format the code before you post, it makes it easier for to follow it. Also if you can create a mock on JSFiddle, it will help in faster resolution Commented Jun 3, 2016 at 8:11
  • Yeah I know but I was in a bit urgent so I posted it sorry plz bear with it Commented Jun 3, 2016 at 8:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.