我试图理解这种行为: 当我更改输入时, View 也会更改,但是当我单击按钮时,警报框不会更改 $scope.firstName。谢谢你给我建议。 问候
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
var test=$scope.firstName;
$scope.test=function(){
alert(test);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
Full Name: {{firstName + " " + lastName}}
<button ng-click="test()">Click</button>
</div>
最佳答案
目前,当您启动 Controller 时,您正在做三件事:
$scope.firstName = "John";
$scope.lastName = "Doe";
var test=$scope.firstName;
因此,您的 var 测试仅在启动 Controller 时初始化,而不是在启动函数时初始化。你的函数,只打印这个值。
如果您想打印示波器的当前版本,您需要执行以下操作:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
$scope.test=function(){
alert($scope.firstName);
};
});
关于angularjs - 为什么警报框中的 $scope 没有变化? ( Angular JS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30530699/