如何根据所选事物覆盖 ng-repeat 数据
How to over write ng-repeat data based on selected thing
var myfriend = angular.module('myfriend',[]);
myfriend.controller('myfriendController', function($scope)
{
$scope.record = [
{ "id" : "01",
"firstname" : "Mohan ",
"middlename" : "K",
"lastname" : "Futterkiste1"
},{
"id" : "04",
"firstname" : "Rohan ",
"middlename" : "A",
"lastname" : "Futterkiste2"
},{
"id" : "08",
"firstname" : "sohan ",
"middlename" : "M",
"lastname" : "Futterkiste3"
}
]
$scope.myemp = function(x)
{
//struck here
// localStorage.setItem('empid',x);
// $location.path('/myView');
// $rootScope.mydetails=true;
// $rootScope.addnewemp=false;
}
});
myfriend.controller('myprofileController', function($scope)
{
$scope.myprofile = [
{ "id" : "01",
"firstname" : "Kristan ",
"middlename" : "Micheal",
"lastname" : "D'souza"
}
]
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
</head>
<body ng-app="myfriend">
My Intital Profile:
<table ng-controller="myprofileController">
<thead>
<tr>
<th>Id</th>
<th>First name</th>
<th>Middle name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in myprofile">
<th>{{x.id}}</th>
<th> {{x.firstname}}</th>
<th>{{x.middlename}}</th>
<th>{{x.lastname}}</th>
</tr>
</tbody>
</table>
<br><br>
<table class="table" style="border:1px red solid; width:100%; " ng-controller="myfriendController">
<thead>
<tr>
<th>Id</th>
<th>First name</th>
<th>Middle name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in record">
<th>{{x.id}}</th>
<th ng-click="selectInfo(x.id)"> {{x.firstname}}</th>
<th>{{x.middlename}}</th>
<th>{{x.lastname}}</th>
</tr>
</tbody>
</table>
<body>
</html>
我有两种看法
myView 和 myfriendView。
在 myView 中,有 my firstname middlename 和 lastname 这三样东西。它来自后端并通过 ng-repeat 显示。
我将我的 ng-repeat 数据保存在
$scope.record = response.data
现在在 View2 中有我的 2 到 3 个朋友的列表。
说 Mohan、Rohan 和 Sohan。
每当我单击任何一个名为 Rohan 的朋友时,我都会获得 Rohan 的所有三个详细信息,然后我将被重定向到视图 1。
我希望现在 myView 数据应该被点击的朋友姓名替换 data.In 这个例子它应该显示 Rohan M D'souze,其中三个字段分别是中间名和姓氏。
我正在将我点击的朋友数据的 ng-repeat 保存在
$scope.record = response.data
问题是,它并没有用点击的好友数据覆盖旧数据。
帮帮我。
当您点击 myfriendController
中的任何朋友时
<th ng-click="selectInfo(x)">
selectInfo(friend)={
this.selectedFriend=friend;
}
现在 myprofileController
只需在 myprofile
中分配这个变量
this.myprofile = this.selectedFriend; // use nav_params for this
如果您将新数据推送到 myprofile 中,它也会保留您的旧数据。所以不要 push 只是赋值。
注意:如果我误解了你的问题,请纠正我。
我已经为你准备了一个工作插件:http://plnkr.co/edit/1zDFB3nSFS12Eixip8rd
大意是利用service在controller之间共享数据
app.service('SelectedFriend', function() {
var SelectedFriend = {
friend: {
"id" : "01",
"firstname" : "Mohan ",
"middlename" : "K",
"lastname" : "Futterkiste1"
} // default value
};
return {
setFriend: function(friend) {
SelectedFriend.friend = friend;
console.log(SelectedFriend.friend);
},
getFriend: function() {
return SelectedFriend.friend;
}
}
});
然后在你的控制器中使用它:
app.controller('myFriendController', function($scope, SelectedFriend) {
// ...
$scope.selectInfo = function(friend) {
SelectedFriend.setFriend(friend);
}
});
app.controller('myProfileController', function($scope, SelectedFriend) {
$scope.myprofile = SelectedFriend;
});
var myfriend = angular.module('myfriend',[]);
myfriend.controller('myfriendController', function($scope)
{
$scope.record = [
{ "id" : "01",
"firstname" : "Mohan ",
"middlename" : "K",
"lastname" : "Futterkiste1"
},{
"id" : "04",
"firstname" : "Rohan ",
"middlename" : "A",
"lastname" : "Futterkiste2"
},{
"id" : "08",
"firstname" : "sohan ",
"middlename" : "M",
"lastname" : "Futterkiste3"
}
]
$scope.myemp = function(x)
{
//struck here
// localStorage.setItem('empid',x);
// $location.path('/myView');
// $rootScope.mydetails=true;
// $rootScope.addnewemp=false;
}
});
myfriend.controller('myprofileController', function($scope)
{
$scope.myprofile = [
{ "id" : "01",
"firstname" : "Kristan ",
"middlename" : "Micheal",
"lastname" : "D'souza"
}
]
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
</head>
<body ng-app="myfriend">
My Intital Profile:
<table ng-controller="myprofileController">
<thead>
<tr>
<th>Id</th>
<th>First name</th>
<th>Middle name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in myprofile">
<th>{{x.id}}</th>
<th> {{x.firstname}}</th>
<th>{{x.middlename}}</th>
<th>{{x.lastname}}</th>
</tr>
</tbody>
</table>
<br><br>
<table class="table" style="border:1px red solid; width:100%; " ng-controller="myfriendController">
<thead>
<tr>
<th>Id</th>
<th>First name</th>
<th>Middle name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in record">
<th>{{x.id}}</th>
<th ng-click="selectInfo(x.id)"> {{x.firstname}}</th>
<th>{{x.middlename}}</th>
<th>{{x.lastname}}</th>
</tr>
</tbody>
</table>
<body>
</html>
我有两种看法
myView 和 myfriendView。
在 myView 中,有 my firstname middlename 和 lastname 这三样东西。它来自后端并通过 ng-repeat 显示。 我将我的 ng-repeat 数据保存在
$scope.record = response.data
现在在 View2 中有我的 2 到 3 个朋友的列表。 说 Mohan、Rohan 和 Sohan。
每当我单击任何一个名为 Rohan 的朋友时,我都会获得 Rohan 的所有三个详细信息,然后我将被重定向到视图 1。
我希望现在 myView 数据应该被点击的朋友姓名替换 data.In 这个例子它应该显示 Rohan M D'souze,其中三个字段分别是中间名和姓氏。
我正在将我点击的朋友数据的 ng-repeat 保存在
$scope.record = response.data
问题是,它并没有用点击的好友数据覆盖旧数据。
帮帮我。
当您点击 myfriendController
<th ng-click="selectInfo(x)">
selectInfo(friend)={
this.selectedFriend=friend;
}
现在 myprofileController
只需在 myprofile
this.myprofile = this.selectedFriend; // use nav_params for this
如果您将新数据推送到 myprofile 中,它也会保留您的旧数据。所以不要 push 只是赋值。
注意:如果我误解了你的问题,请纠正我。
我已经为你准备了一个工作插件:http://plnkr.co/edit/1zDFB3nSFS12Eixip8rd
大意是利用service在controller之间共享数据
app.service('SelectedFriend', function() {
var SelectedFriend = {
friend: {
"id" : "01",
"firstname" : "Mohan ",
"middlename" : "K",
"lastname" : "Futterkiste1"
} // default value
};
return {
setFriend: function(friend) {
SelectedFriend.friend = friend;
console.log(SelectedFriend.friend);
},
getFriend: function() {
return SelectedFriend.friend;
}
}
});
然后在你的控制器中使用它:
app.controller('myFriendController', function($scope, SelectedFriend) {
// ...
$scope.selectInfo = function(friend) {
SelectedFriend.setFriend(friend);
}
});
app.controller('myProfileController', function($scope, SelectedFriend) {
$scope.myprofile = SelectedFriend;
});