Simple AngularJS Form is undefined in Scope

前端 未结 7 1826
独厮守ぢ
独厮守ぢ 2020-12-13 03:33

I\'m starting to play around with AngularJS forms in jsfiddle and I\'ve come across a problem already where a very simple form example is not working as expected. All I have

7条回答
  •  一向
    一向 (楼主)
    2020-12-13 04:03

    The form only registers itself with the $scope of the controller after the controller has initially run. Therefore the console.log($scope.theForm) will return undefined even if everything is setup correctly.

    In your example to react to the presence of theForm, you can setup a watcher on theForm to set debug text depending its presence:

    $scope.$watch('theForm', function(theForm) {
        if(theForm) { 
            $scope.formDebugText = 'Form in Scope';
        }
        else {
            $scope.formDebugText = 'Form is Undefined';
        }        
    });
    

    which can be seen in action at http://jsfiddle.net/9k2Jk/1/

提交回复
热议问题