Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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
Angularjs 如何对模型输入使用指令?_Angularjs - Fatal编程技术网

Angularjs 如何对模型输入使用指令?

Angularjs 如何对模型输入使用指令?,angularjs,Angularjs,我有模型节点: {"id":"593d408cb25f42a812000042","prefix":"array_simple", "title":"Array_simple","type":"3","value":["A","B","C"],"nodes":[]} 在此obejct中,有一个值为数组的字段值: "value":["A","B","C"] 如何使用指令显示数组中每个值的输入,并在更改后将其绑定回模型 我试着写指令: .directive('inputArray', func

我有模型
节点

{"id":"593d408cb25f42a812000042","prefix":"array_simple",
 "title":"Array_simple","type":"3","value":["A","B","C"],"nodes":[]}
在此obejct中,有一个值为数组的字段值:

"value":["A","B","C"]
如何使用指令显示数组中每个值的输入,并在更改后将其绑定回模型

我试着写指令:

.directive('inputArray', function ($compile) {
    return {
        restrict: 'E',
        scope: {
            data: "="
        },

        link: {
            pre: function (scope, element) {

                scope.inputs = [];

                if (angular.isArray(scope.data.value)) {
                    angular.forEach(scope.data.value, function (value) {
                        scope.inputs.push({"type": scope.data.type, "value": value});
                    });

                } else {
                    scope.inputs.push({"type": scope.data.type, "value": scope.data.value});
                }
            }
        },
        template: `<input ng-repeat="value in inputs track by $index" 
                     ng-model="node.value"
                     ng-hide="value.type=='2' || value.type=='6'" 
                     type="text" value="$$value.value$$">`,
        replace: true
    }
})
指令('inputArray',函数($compile){ 返回{ 限制:'E', 范围:{ 数据:“=” }, 链接:{ 前置:功能(范围、要素){ scope.inputs=[]; if(角度isArray(范围数据值)){ 角度.forEach(scope.data.value,函数(value){ push({“type”:scope.data.type,“value”:value}); }); }否则{ push({“type”:scope.data.type,“value”:scope.data.value}); } } }, 模板:``, 替换:正确 } })
但在更改后,它不会将模型更改回原来的状态。

在尝试执行指令之前,请使用模板和控制器:


{{data}}

{{data.value}}