正在获取$ Scope.Form未定义。处理就绪状态的最佳方法是什么,因此可以使用该表单?

问题描述 投票:0回答:5

我有以下控制器,我在这里以'undefined'的形式获得。

angular.module('app').controller('MyController',['$scope', function(){

      $(document).ready(function () { 

      var form = $scope.Form1; // undefined

      }]);
  });

处理就绪状态的最佳方法是什么,因此可以使用该表格?

jquery angularjs angularjs-directive angularjs-scope
5个回答
1
投票

您忘记了将$ scope传递到控制器函数中。 更正:

angular.module('app').controller('MyController',['$scope', function($scope){ //$scope in function here

      $(document).ready(function () { 

      var form = $scope.Form1; // undefined

      }]);
  });

而且,您在这里确实不需要jQuery。


1
投票

尝试替换

'MyController',['$scope', function(){

with

'MyController',['$scope', function($scope){


0
投票

您错过了什么:

  1. 创建角度模块时的空白数组。 angular.module('app', [])
  2. 函数争论$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>

0
投票

语法不是问题,我是即时编写的,这就是为什么是错误代码的原因。

但是我已经通过在指令=>链接中移动此代码解决了该问题,并且我获得了$ scope.Form1。

谢谢


0
投票

您忘记了$scope中的function()自变量

                                             ^
© www.soinside.com 2019 - 2024. All rights reserved.