0
我正在嘗試從控制器中讀取ng-model,但是我無法使其工作。它說ng模型是未定義的。我試圖應用他人提供的解決方案來解決這個類似的問題,但我仍然無法完成工作。也許是因爲我錯了。AngularJS - ng-model在控制器中未定義
我有下面的代碼片段:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.apply = function() {
$scope.name = {}
var firstName = $scope.name.FirstName;
alert(firstName);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-click="apply()" type="button">Apply</button>
Name:
<input ng-model="name.FirstName">
</div>
然而,警報說,變量「名字」點擊「應用」按鈕後不確定。所以我試着改用下面的代碼。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.apply = function() {
var theFirstname = "";
$scope.name = {
theFirstname: $scope.name.FirstName
}
alert(theFirstname);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-click="apply()" type="button">Apply</button>
Name:
<input ng-model="name.FirstName">
</div>
然而,這似乎是我無法分配$scope.name.FirstName
變量 「theFirstname」。