![](/img/trans.png)
[英]How can I append key value pair to the url which already existed key value pair in AngularJS?
[英]Append key value pair to angularjs object
我正在從 laravel 5.2 表單請求驗證中獲取 json 輸出
我的 json 輸出是:
{
"title": [
"The title field is required."
],
"description": [
"The description field is required."
],
"address.city": [
"City field is required"
]
}
將此輸出附加到對象
var self = this;
self.error = {
address: {}
};
var onFailure = function (response) {
angular.forEach(response.data, function(value, key) {
self.error[key] = value[0];
});
};
我想在我的視圖中訪問這個錯誤對象,對於“address.city”,我無法使用“ctrl.error.address.city”訪問它,其余的我可以訪問
如何使用包含“.”的鍵附加對象並在視圖中顯示它?
這是你需要的。 但最好不要在屬性名稱中包含 (.)。 相反,您可以使用下划線 (_)。 避免在屬性名稱中使用 dot(.) 作為字符。
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>
如果您知道密鑰名稱,那么您可以訪問它的唯一方法是通過[]
。
檢查小提琴以獲取更多信息。
https://jsfiddle.net/9f4mbh1h/1/
您需要將代碼更改為此
在你的控制器中
myApp.controller('MyCtrl', function ($scope) {
$scope.foo={};
$scope.foo "hi";
});
在 html 中
<div ng-app="MyApp" ng-controller="MyCtrl">
<input type="text" ng-model="foo" /> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.