获取 AngularJS 中的编辑值
Get the edited value in AngularJS
我有 JSON 格式的数据,如下所示:
"details": {
"col1": "value1",
"col2": "value2",
"col3": "value3",
"col4": "value4",
"col5": "value5",
"col6": "value6",
"col7": "value7",
"col8": "value8"
}
<div ng-repeat="(key,value) in details">
<div>{{key}}:</div>
<input value="value">{{value}} </input>
如果我编辑了字段值,如何在控制器中获取编辑后的值?
使用 $scope 变量并绑定到 ng-model
<input type="text" ng-model="value" ng-blur="print(value)" size="30">
var app = angular.module('myApp', []);
app.controller('appCtrl', function($scope) {
var details = {
"col1": "value1",
"col2": "value2",
"col3": "value3",
"col4": "value4",
"col5": "value5",
"col6": "value6",
"col7": "value7",
"col8": "value8"
}
$scope.details = details;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="appCtrl">
<div ng-repeat="(key,value) in details">
<label>{{key}}:</label>
<input ng-model="details[key]">
</div>
<br>
<br>
<div>{{details}}</div>
</body>
这是您要找的吗?
试试这个有效的片段。它将根据文本框中的更改给出值。
var app = angular.module('myApp', []);
app.controller('appCtrl', function($scope) {
var details = {
"col1": "value1",
"col2": "value2",
"col3": "value3",
"col4": "value4",
"col5": "value5",
"col6": "value6",
"col7": "value7",
"col8": "value8"
}
$scope.details = details;
$scope.getChangedValue = function(value){
console.log("Key :" + value);
console.log("Value :" + details[value]);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="appCtrl">
<div ng-repeat="(key,value) in details">
<label>{{key}}:</label>
<input ng-model="details[key]" ng-blur="getChangedValue(key)">
</div>
<br>
<br>
<div>{{details}}</div>
</div>
</body>
@Mahesh Karthu:谢谢你的片段。
我有 JSON 格式的数据,如下所示:
"details": {
"col1": "value1",
"col2": "value2",
"col3": "value3",
"col4": "value4",
"col5": "value5",
"col6": "value6",
"col7": "value7",
"col8": "value8"
}
<div ng-repeat="(key,value) in details">
<div>{{key}}:</div>
<input value="value">{{value}} </input>
如果我编辑了字段值,如何在控制器中获取编辑后的值?
使用 $scope 变量并绑定到 ng-model
<input type="text" ng-model="value" ng-blur="print(value)" size="30">
var app = angular.module('myApp', []);
app.controller('appCtrl', function($scope) {
var details = {
"col1": "value1",
"col2": "value2",
"col3": "value3",
"col4": "value4",
"col5": "value5",
"col6": "value6",
"col7": "value7",
"col8": "value8"
}
$scope.details = details;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="appCtrl">
<div ng-repeat="(key,value) in details">
<label>{{key}}:</label>
<input ng-model="details[key]">
</div>
<br>
<br>
<div>{{details}}</div>
</body>
这是您要找的吗?
试试这个有效的片段。它将根据文本框中的更改给出值。
var app = angular.module('myApp', []);
app.controller('appCtrl', function($scope) {
var details = {
"col1": "value1",
"col2": "value2",
"col3": "value3",
"col4": "value4",
"col5": "value5",
"col6": "value6",
"col7": "value7",
"col8": "value8"
}
$scope.details = details;
$scope.getChangedValue = function(value){
console.log("Key :" + value);
console.log("Value :" + details[value]);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="appCtrl">
<div ng-repeat="(key,value) in details">
<label>{{key}}:</label>
<input ng-model="details[key]" ng-blur="getChangedValue(key)">
</div>
<br>
<br>
<div>{{details}}</div>
</div>
</body>
@Mahesh Karthu:谢谢你的片段。