大家好,我正在尝试使用 Angular ng-bind 从文本字段获取一些输入,并在 Controller 中访问它。目前,当我尝试打印它时,我得到了未定义的信息。希望得到一些帮助,我确信我的设置中缺少一些东西。
这是我的 HTML
<div class="text-center rounded-box" ng-app="AuthorisationTicketApp" ng-controller="LookupTicketController">
<form>
<b>Please insert text:</b>
<input type="text" name="text" ng-bind="text" />
</form>
<div class="lg-margin-top">
<a href="#lookup-ticket" class="btn btn-lg btn-primary">Submit</a>
</div>
我的 Controller
authorisationTicketApp.controller('LookupTicketController', ['$scope',
function($scope) {
console.log('LookupTicketController .... called');
console.log('text' + $scope.text);
console.log('$scope ' + $scope);
}]);
我的app.js
var authorisationTicketApp = angular.module('AuthorisationTicketApp', ['ngRoute']);
authorisationTicketApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/input-text', {
templateUrl: contextPath + 'partials/input-text.html',
controller: 'LookupTicketController'
}).
when('/lookup-ticket', {
templateUrl: contextPath + 'partials/lookup-ticket.html',
controller: 'LookupTicketController'
}).
otherwise({
redirectTo: '/'
});
}]);
我的控制台输出
LookupTicketController .... called controllers.js:11:1
$ text undefined controllers.js:13:1
$scope [object Object]
最佳答案
在输入标记上使用 ng-model
而不是 ng-bind
。这将双向绑定(bind)到 Controller 中的值:
<input type="text" name="text" ng-model="text" />
关于javascript - 无法使用 Angular 从 HTML View 获取值到 Controller 。解决方案返回$scope变量未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32206758/