Angularjs 如何访问控制器函数中的变量?
我希望能够访问此控制器函数中的控制器变量 为什么下面的代码向控制台抛出错误而不是显示输入值 JS: HTML:Angularjs 如何访问控制器函数中的变量?,angularjs,angularjs-scope,Angularjs,Angularjs Scope,我希望能够访问此控制器函数中的控制器变量 为什么下面的代码向控制台抛出错误而不是显示输入值 JS: HTML: <div ng-app="app" ng-controller="TestCtrl"> <input type="text" ng-model="string"></input> <button ng-click="click()">Click me !</button> </div> 点击我!
<div ng-app="app" ng-controller="TestCtrl">
<input type="text" ng-model="string"></input>
<button ng-click="click()">Click me !</button>
</div>
点击我!
从单击函数中的参数中删除$scope,并在函数中使用它-请参见下文和
您可以尝试另一种方法:
app.controller("TestCtrl", function($scope) {
this.string = "";
var that = this;
$scope.click = function() {
console.debug(that.string);
}
});
请参见JSFIDLE上的:
app.controller("TestCtrl", function($scope) {
$scope.string = "";
$scope.click = function() {
console.debug($scope.string);
}
});
app.controller("TestCtrl", function($scope) {
this.string = "";
var that = this;
$scope.click = function() {
console.debug(that.string);
}
});