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
Javascript 在ng repeat循环和双向数据绑定之外访问ng repeat的子范围_Javascript_Angularjs - Fatal编程技术网

Javascript 在ng repeat循环和双向数据绑定之外访问ng repeat的子范围

Javascript 在ng repeat循环和双向数据绑定之外访问ng repeat的子范围,javascript,angularjs,Javascript,Angularjs,我有一个ng repeat,在每个重复的li中,我尝试放置ng模型,稍后我希望在ng repeat之外进行双向数据绑定。请参见下面的jsbin 我知道有很多关于ng模型和ng repeat的问题得到了回答。但我无法找到所需的解决方案 如果有人有足够的耐心来理解我的问题,这将有助于学习angularjs 相关代码: (函数(){ var-app=angular.module(“myApp”,[]); }()); (功能(){ var myCtrl=函数($scope){ $scope.sele

我有一个ng repeat,在每个重复的li中,我尝试放置ng模型,稍后我希望在ng repeat之外进行双向数据绑定。请参见下面的jsbin

我知道有很多关于ng模型和ng repeat的问题得到了回答。但我无法找到所需的解决方案

如果有人有足够的耐心来理解我的问题,这将有助于学习angularjs

相关代码:

(函数(){
var-app=angular.module(“myApp”,[]);
}());
(功能(){
var myCtrl=函数($scope){
$scope.selection_add={};
$scope.selection_edit={};
$scope.selection_delete={};
$scope.categories=[
{“名称”:“运动”,“id”:“50d5ad”},
{“名称”:“一般”,“id”:“687ffr”},
{“名称”:“活动”,“id”:“678ffb”},
{“姓名”:“问候”,“身份证”:“678fff”},
{“姓名”:“谢谢”,“身份证”:“678fer”},
{“name”:“Goes”,“id”:“678fgf”},
{“名称”:“机会”,“id”:“674ffr”},
{“名称”:“说服”,“id”:“654ffr”},
{“名称”:“Mopols”,“id”:“623ffr”}
];
$scope.setCurrentCategory=函数setCurrentCategory(类别){
$scope.currentCategory=类别;
};
};
角度。模块(“myApp”)
.controller(“myCtrl”,myCtrl);
}());

JS-Bin
添加
编辑
删除
{{category.name}
{{category.name}
{{category.name}
为{{currentCategory.name}添加
为{{currentCategory.name}编辑
{{selection_edit[currentCategory.id]}
删除{{currentCategory.name}
{{selection_delete[currentCategory.id]}

使用对象中的属性作为每个
输入的模型,然后尝试显示输入的数据。但是,当您尝试显示数据时,不会将模型指向正在更改的特性。您只需使用整个对象

为了解决这个问题,我添加了一个保存
currentCategory
,然后根据它显示所有数据的点击

对html的更改:

    <tr ng-repeat="category in categories" ng-click="setCurrentCategory(category)"> <-- note the ng-click that saves currentCategory -->


    <!-- currentCategory is used to change the model to our currentCategory, and display the category name -->
    <h1>Add for {{currentCategory.name}}</h1>
    <input type="text" ng-model="selection_add[currentCategory.id]">
    <h1>Edit for {{currentCategory.name}}</h1>
    {{selection_edit[currentCategory.id]}}
    <h1>Delete for {{currentCategory.name}}</h1>
    {{selection_delete[currentCategory.id]}}

您将中的值从HTML传递到控制器的方式是错误的。我不明白,您能解释一下吗?例如,用户添加???到运动类别,结果是???发生,编辑和删除也是如此。
$scope.setCurrentCategory = function setCurrentCategory(category) {
  $scope.currentCategory = category;
};