我想在此示例angularjs示例中提醒隐藏值。但不提醒隐藏价值。为什么呢?
function TestController($scope) {
$scope.id = "";
$scope.test = function () {
alert($scope.id)
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="TestController">
<input type="hidden" name="id" ng-value="12"></input>
<input type="submit" ng-click="test()" value="alertHiddenValue"></input>
</div>
答案 0 :(得分:1)
ng-value
不是您在这里寻找的东西。在Angular中,您使用ng-model
将控制器值绑定到视图元素的值,如下所示:
function TestController($scope) {
$scope.id = "12"; // Initial value can be set here
$scope.test = function () {
alert($scope.id);
};
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="TestController">
<!-- ngModel is used to two-way bind between your value & your controller -->
<input type="hidden" name="id" ng-model="id"></input>
<input type="submit" ng-click="test()" value="alertHiddenValue"></input>
</div>
&#13;
答案 1 :(得分:-1)
您需要使用ng-model将值绑定到id,而不是使用ng-value使用ng-init来初始化它。另外正如@RGraham所提到的,如果你想拥有默认值,请尝试在控制器中初始化它。
ng-value用于绑定值或输入[radio / checkbox],see here。
function TestController($scope) {
$scope.id = "";
$scope.test = function () {
alert($scope.id)
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="TestController">
<input type="hidden" name="id" ng-model="id" ng-init="id=12"></input>
<input type="submit" ng-click="test()" value="alertHiddenValue"></input>
</div>