1

I am getting json output from laravel 5.2 form request validation

My json output is:

{
    "title": [
        "The title field is required."
    ],
    "description": [
        "The description field is required."
    ],
    "address.city": [
        "City field is required"
    ]
}

Appending this output to object

var self = this;
self.error = {
    address: {}
};

var onFailure = function (response) {
    angular.forEach(response.data, function(value, key) {
        self.error[key] = value[0];
    });
};

I want to access this error object to my view, for "address.city", I can't access it using "ctrl.error.address.city", rest I can access

How to append object with key containing "." and show it in view?

3 Answers 3

1

Here is what you need. But its better not to have (.) in a property name. Instead you can use a underscore(_). Avoid using dot(.) as a chaaracter in property names.

var myApp = angular.module('MyApp', []);

myApp.controller('MyCtrl', function ($scope) {
 $scope.foo={};
    $scope.foo['hello.There'] = "I'm foo!";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyCtrl">
    {{foo["hello.There"]}}<br />
    <input type="text" ng-model="foo['hello.There']" />
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

if you know the key name then the only way you can access it is by [].

Check the fiddle for more info.

https://jsfiddle.net/9f4mbh1h/1/

Comments

0

You need to change your code to this

in your controller

myApp.controller('MyCtrl', function ($scope) {
 $scope.foo={};
$scope.foo "hi";
  });

in html

<div ng-app="MyApp" ng-controller="MyCtrl">
  <input type="text" ng-model="foo" /> </div>

Comments

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.