Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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 使用ng model指令从动态创建的标记中提取数据_Javascript_Angularjs - Fatal编程技术网

Javascript 使用ng model指令从动态创建的标记中提取数据

Javascript 使用ng model指令从动态创建的标记中提取数据,javascript,angularjs,Javascript,Angularjs,是否可以多次使用同一ng模型指令并以唯一方式访问它们 我有一个指令,用单个模板的多个实例填充页面。因为它是同一个模板,所以添加的所有ng模型属性都指向控制器中的同一个变量 我目前正在使用document.getElementsByName,因此我得到了一个可以迭代的列表,但是当试图以这种方式获取数据时,与简单地引用控制器中的变量相比,会有更多的混乱 编辑: 此HTML: <div class="row"> <tib-copy></tib-copy> &

是否可以多次使用同一ng模型指令并以唯一方式访问它们

我有一个指令,用单个模板的多个实例填充页面。因为它是同一个模板,所以添加的所有ng模型属性都指向控制器中的同一个变量

我目前正在使用document.getElementsByName,因此我得到了一个可以迭代的列表,但是当试图以这种方式获取数据时,与简单地引用控制器中的变量相比,会有更多的混乱

编辑:

此HTML:

<div class="row">
    <tib-copy></tib-copy>
</div>
获取注入并变为:

<tib-copy>
    <fieldset class="col-md-2" style="margin-bottom: 10px">
        <legend>Copy</legend>

        <input type="text" ng-model="searchOptions.sourceServer">
        <br/>
        <input type="text" ng-model="searchOptions.sourcePath">
        <br/>
        <input type="text" ng-model="searchOptions.destServer">
        <br/>
        <input type="text" ng-model="searchOptions.destPath">
    </fieldset>
</tib-copy>
再次单击一个按钮调用函数通过ng click执行注入,将导致相同的ng模型标记在DOM中重复


例如,我希望将searchOptions.sourceServer的所有ng模型属性作为列表或其他允许我单独提取值的内容获取。但是,由于双向绑定,所有文本字段都使用相同的值进行更新。

为此,您需要将ngModelController用于tibCopy指令。这意味着您在其中定义的每个模型都充当本地指令模型。然后,您必须将它们解析为更高级别的ngModel,然后将指令呈现到其他组件中

例如,可以从以下指令结构开始:

应用指令“tbpCopy”, 作用{ 返回{ 限制:'E', 要求:'ngModel', 模板URL:+ “复制”+ + + + + , 链接:函数$scope、$element、$attrs、$model{ $model.$formatters.pushfunctionmodelValue{ ifmodelValue{ 返回{ 搜索选项:{ sourceServer:modelValue.searchOptions.sourceServer, sourcePath:modelValue.searchOptions.sourcePath, destServer:modelValue.searchOptions.destServer, destPath:modelValue.searchOptions.destPath } }; } }; $model.$render=函数{ 如果$model.$viewValue{ $scope.searchOptions={ sourceServer:$model.$viewValue.searchOptions.sourceServer, sourceServer:$model.$viewValue.searchOptions.sourcePath, sourceServer:$model.$viewValue.searchOptions.destServer, sourceServer:$model.$viewValue.searchOptions.destPath } } }; $model.$parsers.pushfunctionviewValue{ ifviewValue{ 返回{ 搜索选项:{ sourceServer:viewValue.searchOptions.sourceServer, sourcePath:viewValue.searchOptions.sourcePath, destServer:viewValue.searchOptions.destServer, destPath:viewValue.searchOptions.destPath } }; } }; } };
};你能提供html/模板代码吗。你是在ng repeat中创建这些标签吗?@The.Bear我添加了一个示例哇,看起来很棒。您是否有一个链接,指向您用来了解您所展示的详细指令的资源?变量$model.$formatters、$model.$render和$model.$parsers的信息也一样?我看的地方往往很模糊,只使用AngularJS的结构,没有解释。这可以在AngularJS的文档页面上找到:谢谢。事实证明,我不需要指令,但在我看的地方,它们似乎是解决方案,所以我不知道一个更简单的解决方案。我使用ng repeat和数据到模型数组解决了这个问题,避免了添加任何DOM元素。