Simple AngularJS Form is undefined in Scope

前端 未结 7 1792
独厮守ぢ
独厮守ぢ 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:20

    A good way to perform this without using watch (which is a bit overkill) is to define an object in the scope into which you will register the form.

    HTML

    <div id="mainContainer" ng-app="angularTest" ng-controller="MainCtrl">
        <h1>The Form</h1>
        <form name="form.theForm">
            <input name="myName" type="text" ng-model="model.name" />
            <input type="button" value="Here the scope" ng-click="display()"/>
            <input name="submit" type="submit" />
        </form>
    </div>
    

    JS

    var app = angular.module('angularTest', []);
    
    app.controller('MainCtrl', ['$scope', function($scope) {
        $scope.model = { name: 'Model Name' };
        $scope.form = {};
        $scope.display = function () {
            console.log($scope.form.theForm);
        }
    }]);
    
    0 讨论(0)
提交回复
热议问题