Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度-提取指令';引导上的属性值_Javascript_Angularjs - Fatal编程技术网

Javascript 角度-提取指令';引导上的属性值

Javascript 角度-提取指令';引导上的属性值,javascript,angularjs,Javascript,Angularjs,我有一个表单,它由一些已知的输入和一些从我不控制但可以期望正确编写的部分中包含的输入组成。它看起来是这样的: <form ...> <input name="input1" type="hidden" value="1"> <div class="from_partial" ng-controller="Controller"> <!-- here goes first partial --> <button type

我有一个表单,它由一些已知的输入和一些从我不控制但可以期望正确编写的部分中包含的输入组成。它看起来是这样的:

<form ...>
  <input name="input1" type="hidden" value="1">
  <div class="from_partial" ng-controller="Controller">
    <!-- here goes first partial -->
    <button type="button" ng-click="check()">Check</button>
  </div>
  <div class="from_partial" ng-controller="Controller">
    <!-- here goes second partial -->
    <button type="button" ng-click="check()">Check</button>
  </div>
</form>
<div class="item">
  <input type="text" ng-model="name"/>
</div>
<div class="item">
  <input type="text" ng-model="age"/>
</div>

检查
检查
看起来像这样:

<form ...>
  <input name="input1" type="hidden" value="1">
  <div class="from_partial" ng-controller="Controller">
    <!-- here goes first partial -->
    <button type="button" ng-click="check()">Check</button>
  </div>
  <div class="from_partial" ng-controller="Controller">
    <!-- here goes second partial -->
    <button type="button" ng-click="check()">Check</button>
  </div>
</form>
<div class="item">
  <input type="text" ng-model="name"/>
</div>
<div class="item">
  <input type="text" ng-model="age"/>
</div>

现在我需要在控制器的
check()
函数中分别为每个部分使用这些输入。我事先不知道他们的名字。列举它们的最好方法是什么?Angular正在引导特定型号并使用其
$scope
连接时,是否可以获取它们(即找到它们的名称)


换句话说,我希望每个
控制器
对象都知道输入的名称,例如
$scope.input_names
包含
{“name”,“age”}

可以用相同的名称定义多个指令,这样就很容易了:

angular.module(app_name)
.controller("Controller", ['$scope', function ($scope) {
    $scope.input_names = [];
    ...
}])
.directive('ngModel', function() {
    var linker = function(scope, element, attrs) {
        scope.input_names.push(attrs.ngModel);
    }

    return {
        restrict: 'A',
        priority: 100,
        require: '?ngModel',
        link: linker
    };
});

原始角度指令定义为优先级1,将首先执行。然后,自定义指令将使用
ng model
属性定义的名称插入到
scope.input\u names
。这确保每个控制器将只获取在相关部分中定义的名称。与ng模型相关的所有其他功能都将正常工作。

事实证明,可以用相同的名称定义多个指令,这样就很容易了:

angular.module(app_name)
.controller("Controller", ['$scope', function ($scope) {
    $scope.input_names = [];
    ...
}])
.directive('ngModel', function() {
    var linker = function(scope, element, attrs) {
        scope.input_names.push(attrs.ngModel);
    }

    return {
        restrict: 'A',
        priority: 100,
        require: '?ngModel',
        link: linker
    };
});
原始角度指令定义为优先级1,将首先执行。然后,自定义指令将使用
ng model
属性定义的名称插入到
scope.input\u names
。这确保每个控制器将只获取在相关部分中定义的名称。与
ng model
相关的所有其他内容都将正常工作。

如果表单具有“name”属性,则可以在
$scope.formName
对象中获取它的所有模型。如果表单具有“name”属性,则可以在
$scope.formName
对象中获取它的所有模型。