如何使用 ng-model 在输入字段中显示值?
How to show value in input field with ng-model?
我创建了编辑表单,所以我必须为每个文本字段包含默认值。我写了这个 HTML 代码:
<input type="text" ng-model="projectData.title" ng-init="projectData.title=title" name="title" class="form-control" />
在控制器中:
$scope.title = "This is just title";
文本框中什么也没有显示。我尝试了 ng-init="projectData.title={{title}}"
和 ng-init="projectData.title='title'"
但它就是不起作用。
我正在使用 angularjs 1.6,但以下解决方案也不起作用。
Check This [Code][1]
[1]: http://jsfiddle.net/d4ts76ys/
使用对象将其映射到ng-model
在您的范围内,您应该像这样声明对象:
$scope.projectData = {};
$scope.projectData.title = "This is just title";
嗯,你做错了一点。
ng-init 是 angular 指令,您不需要花括号。
将您的代码修改为:
html:
<div ng-controller="MyCtrl">
<input type="text" ng-init="rootFolders.name=name" ng-
model="rootFolders.name" >
<br>rootFolders={{name}}
</div>
js:
var app = angular.module('myApp',[]);
app.controller('MyCtrl', function($scope) {
$scope.name = "Acunisasi";
});
I have created fiddle that may help your
//html
<div ng-controller='MyCtrl'>
<form>
<input ng-init="projectData.title = title" type="text" ng-model="title">
<button ng-click="formSubmit()">
submit
</button>
</form>
{{title}}
</div>
//js
var app = angular.module('myApp', []);
app.controller('MyCtrl', ['$scope', MyController]);
function MyController($scope) {
$scope.projectData = {};
$scope.title = 'This is just title';
$scope.formSubmit = function() {
console.log("$scope.projectData ===>", $scope.projectData)
}
}
我创建了编辑表单,所以我必须为每个文本字段包含默认值。我写了这个 HTML 代码:
<input type="text" ng-model="projectData.title" ng-init="projectData.title=title" name="title" class="form-control" />
在控制器中:
$scope.title = "This is just title";
文本框中什么也没有显示。我尝试了 ng-init="projectData.title={{title}}"
和 ng-init="projectData.title='title'"
但它就是不起作用。
我正在使用 angularjs 1.6,但以下解决方案也不起作用。
Check This [Code][1]
[1]: http://jsfiddle.net/d4ts76ys/
使用对象将其映射到ng-model
在您的范围内,您应该像这样声明对象:
$scope.projectData = {};
$scope.projectData.title = "This is just title";
嗯,你做错了一点。
ng-init 是 angular 指令,您不需要花括号。
将您的代码修改为: html:
<div ng-controller="MyCtrl">
<input type="text" ng-init="rootFolders.name=name" ng-
model="rootFolders.name" >
<br>rootFolders={{name}}
</div>
js:
var app = angular.module('myApp',[]);
app.controller('MyCtrl', function($scope) {
$scope.name = "Acunisasi";
});
I have created fiddle that may help your
//html
<div ng-controller='MyCtrl'>
<form>
<input ng-init="projectData.title = title" type="text" ng-model="title">
<button ng-click="formSubmit()">
submit
</button>
</form>
{{title}}
</div>
//js
var app = angular.module('myApp', []);
app.controller('MyCtrl', ['$scope', MyController]);
function MyController($scope) {
$scope.projectData = {};
$scope.title = 'This is just title';
$scope.formSubmit = function() {
console.log("$scope.projectData ===>", $scope.projectData)
}
}