表单元素没有出现在$范围出现在、表单、元素、范围

2023-09-13 04:33:54 作者:暖阳

下面是我的形式开始...

Here's the start of my form...

<div ng-form name="CustomerForm" ng-controller="Customer">

下面是我的控制器......

Here's my controller...

app.controller('Customer', ['$scope', function ($scope) {

   alert($scope.CustomerForm);
}]);

$ scope.CustomerForm 是不确定的。不应形式加入到范围?

$scope.CustomerForm is undefined. Shouldn't the form be added to scope?

推荐答案

在你的警报语句的时候,CustomerForm不是 $范围尚未以内。

At the time of your alert statement, CustomerForm isn't within $scope yet.

控制器是为了:

创建一个范围对象的初始状态。添加行为的范围对象。

这里更多约控制器。

Read more here about controllers.

在这里看到: DEMO

See here: DEMO

JS:

var app = angular.module('myApp',[]);

app.controller('Customer', ['$scope', function ($scope) {

    $scope.getFormName = function(){
        console.log($scope.CustomerForm.$name);
    }
}]);

HTML:

<div ng-form name="CustomerForm" ng-controller="Customer">
    <button ng-click="getFormName()">CLICK</button>
</div>