我有以下控制器,我在这里以'undefined'的形式获得。
angular.module('app').controller('MyController',['$scope', function(){
$(document).ready(function () {
var form = $scope.Form1; // undefined
}]);
});
处理就绪状态的最佳方法是什么,因此可以使用该表格?
您忘记了将$ scope传递到控制器函数中。 更正:
angular.module('app').controller('MyController',['$scope', function($scope){ //$scope in function here
$(document).ready(function () {
var form = $scope.Form1; // undefined
}]);
});
而且,您在这里确实不需要jQuery。
尝试替换
'MyController',['$scope', function(){
with
'MyController',['$scope', function($scope){
您错过了什么:
angular.module('app', [])
。$scope
中的[function($scope){
。只需创建一个适当的选择器,然后在func参数中传递$scope
:
angular.module('app', []).controller('MyController',['$scope', function($scope){ // <---pass $scope here
$scope.Form1 = document.querySelector('form')
}]);
示例测试如下:
angular.module('testApp', []).controller('formController',['$scope', function($scope){
$scope.form = document.querySelector('form');
$scope.getValue= function(){
alert($scope.form.id);
}
}]);
<div ng-app='testApp'>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script>
<form ng-controller='formController' id='testForm'>
<input type='text' placeholder='dummy text to be placed' autofocus ng-focus='getValue()'>
</form>
</div>
语法不是问题,我是即时编写的,这就是为什么是错误代码的原因。
但是我已经通过在指令=>链接中移动此代码解决了该问题,并且我获得了$ scope.Form1。
谢谢
您忘记了$scope
中的function()
自变量
^