Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.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/1/angularjs/24.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模型值_Javascript_Angularjs - Fatal编程技术网

从javascript设置ng模型值

从javascript设置ng模型值,javascript,angularjs,Javascript,Angularjs,是否有任何方法可以使用setAttribute方法或任何其他方式设置输入文本框的ng模型值。 我有一个按钮,通过在html中克隆文本类型输入来添加textbox。但是我不知道如何在创建文本框时设置ng模型,而是从按钮的onclick方法克隆出来的。 PS:创建不同的文本框时,ng模型的值是不同的 更新 var add=函数(){ var txtClone=document.getElementById(“textinput”).cloneNode(true); setAttribute(“ng

是否有任何方法可以使用setAttribute方法或任何其他方式设置输入文本框的ng模型值。 我有一个按钮,通过在html中克隆文本类型输入来添加textbox。但是我不知道如何在创建文本框时设置ng模型,而是从按钮的onclick方法克隆出来的。 PS:创建不同的文本框时,ng模型的值是不同的

更新

var add=函数(){
var txtClone=document.getElementById(“textinput”).cloneNode(true);
setAttribute(“ng模型”、“名称”);
document.getElementById(“目标”).appendChild(txtClone);
}
在输入框中输入一些内容:

名称:


类型 你好{{name}
在这里,当单击按钮(类型)时,我克隆了div with ng应用程序外部的输入文本,

在那里,我尝试用setAttribute设置克隆文本框的ng模型值。。。。但它并不具有约束力。我的问题是,有没有办法从js脚本中设置这个ng模型值?

ng模型
值通常只是范围变量,您可以在控制器中修改

例如:

在你看来:

在控制器中:

app.controller('textBoxController', function () {

    this.textBoxList = [];

    this.addTextBox = function () {
        this.textBoxList.push({
            value: 0
        })
    }.bind(this)

});
$scope.demoInput=”https://www.youtube.com/watch?v=dQw4w9WgXcQ";

在该示例中,无论何时更改
$scope.demoInput
,文本框中的值都将更新以反映它

即使输入是在值数组上迭代生成的
ng repeat
,这种方法仍然有效

<div ng-repeat="text in demoInputs">
    <input type="text" ng-model="text"></input>
</div>


将与

for(var i=0;i<somelength;i++)
    $scope.demoInputs[i] = 'At index ' + i;

for(var i=0;i好的,如果我理解正确,您基本上是在向输入列表中添加文本框

您可以做以下几件事:

对象路径列表

假设您拥有以下控制器:

app.controller('textBoxController', function () {

    this.textBoxList = [];

    this.addTextBox = function () {
        this.textBoxList.push({
            value: 0
        })
    }.bind(this)

});
以及以下html:

<div ng-controller="textBoxController as txtCTRL">

    <div ng-repeat="item in txtCTRL.textBoxList">
       <input type="text" ng-model="item.value">

    </div>

</div>


你能给我一个你目前拥有的代码示例吗?你有JSFIDLE或PLUNK示例吗?但是动态内容可以作为模板添加在你的更新中:你不使用指令或控制器有什么原因吗?没有,没有明确的原因……我只是上传了一个简短的代码……我只是问有什么办法我可以设置ng模型,就像使用setAttribute设置元素的属性一样??您可以提供代码的JSFIDLE或plunkr吗??
<div ng-controller="textBoxController as txtCTRL">

    <div ng-repeat="item in txtCTRL.textBoxList">
       <input type="text" ng-model="item.value">

    </div>

</div>