<div ng-repeat="user in children">
<a ng-click="swapUser(user.user_id);"></a>
<div ng-model="player.name" currentuser="user.user_id" inline-edit="parentUser.name" on-save="log('SAVE', value)" on-cancel="log('CANCEL', value)"></div>
</div>
Directive :
return {
scope: {
model: '=inlineEdit',
handleSave: '&onSave',
currentuser: '=',
handleCancel: '&onCancel'
},
link: function(scope, elm, attr) {
var currentuser = attr.currentuser;
console.log("Userid", attr.currentuser);
//Here I want to set the user_id so that i can update respective users
scope.save = function() {
var objInput = {};
objInput[field] = scope.model;
user.updateConfirmUser(objInput, currentuser)
.success(function () {
console.log("Saved");
});
}
Controller :
$scope.children = [
{
"name" : "Amy",
"age" : "50",
"height" : "5",
"type" : "2",
"user_id" : "1"
},{
"name" : "Rock",
"age" : "50",
"height" : "5",
"type" : "2",
"user_id" : "2"
},{
"name" : "Jimmy",
"age" : "50",
"height" : "5",
"type" : "3",
"user_id" : "3"
},{
"name" : "Michael",
"age" : "50",
"height" : "5",
"type" : "3",
"user_id" : "4"
}
];
How can i access the currentuser into my directive from html i setup current user to attr and trying to access into directive it says undefined user_id. Please take a look above code and suggest thanks
You are binding currentuser with value of "user.user_id" so only the user_id will be passed.
Try to pass the entire object: currentuser="user"
Related
I have a collection where each document has an array of sub-documents, containing several fields. I need to update specific sub-documents based on their fields, but every attempt I've done so far changes the field in all of the arrayed sub-documents
Here is some sample data:
{
'_id' : ObjectId('0001'),
'Region' : 'Northern',
"Items" : [
{
"ItemId" : NumberInt(25),
"Name" : "Widget",
"ProductType" : "15",
"ItemIdLegacy" : "ca-000037"
},
{
"ItemId" : NumberInt(30),
"Name" : "Gizmo",
"ProductType" : "15",
"ItemIdLegacy" : "ca-000038"
},
{
"ItemId" : NumberInt(35),
"Name" : "Thingy",
"ProductType" : "15",
"ItemIdLegacy" : "ca-000039"
}
]
}
When I try to use update() with the following query, it updates the ProductType on all array items, not just the one I'm trying to change.
To clarify, I want to change the array item with ItemIdLegacy: "ca-000038" to have ProductType: 20. All other Array items should remain unchanged. Query that I have tried:
db.Collection.update({"Items.ItemIdLegacy" : "ca-000038"},[{$set: { "Items.ProductType" : "20"} }],{multi: false});
This is the desired output:
{
'_id' : ObjectId('0001'),
'Region' : 'Northern',
"Items" : [
{
"ItemId" : NumberInt(25),
"Name" : "Widget",
"ProductType" : "15",
"ItemIdLegacy" : "ca-000037"
},
{
"ItemId" : NumberInt(30),
"Name" : "Gizmo",
"ProductType" : "20",
"ItemIdLegacy" : "ca-000038"
},
{
"ItemId" : NumberInt(35),
"Name" : "Thingy",
"ProductType" : "15",
"ItemIdLegacy" : "ca-000039"
}
]
}
But this is the actual output of running that query:
{
'_id' : ObjectId('0001'),
'Region' : 'Northern',
"Items" : [
{
"ItemId" : NumberInt(25),
"Name" : "Widget",
"ProductType" : "20",
"ItemIdLegacy" : "ca-000037"
},
{
"ItemId" : NumberInt(30),
"Name" : "Gizmo",
"ProductType" : "20",
"ItemIdLegacy" : "ca-000038"
},
{
"ItemId" : NumberInt(35),
"Name" : "Thingy",
"ProductType" : "20",
"ItemIdLegacy" : "ca-000039"
}
]
}
I feel like I'm missing something simple...
EDIT: I have q uery that will allow me to update a single matching element in the array, but I have thousands that would need to be done so one offs don't necessarily work (sure I could parse them all out and spam them to the shell, but I want something more elegant)
Single Element Update:
db.Collection.updateOne({"Items.ItemIdLegacy" : "ca-000038"},{ $set: { "Items.$.ProductType" : "20" } } );
Try this :-
db.collection.update({
"Items.ItemIdLegacy": "ca-000038"
},
{
$set: {
"Items.$.ProductType": 20
}
},
{
multi: true
})
Here is Working Example
I'm developing a mobile app using the Ionic Framework. I have an array of objects as described here (app.js):
$scope.versesList = [
{id:"1" , value:'5', checked:($scope.selectedItem==this)} ,
{id:"2" , value:'10', checked:($scope.selectedItem==this)} ,
{id:"3" , value:'15', checked:($scope.selectedItem==this)} ,
{id:"4" , value:'20', checked:($scope.selectedItem==this)}
];
$scope.selectedItem = $scope.versesList[1];
Here's the code that produces my output :
<div class="item">
<pre ng-bind="versesList | json"></pre>
</div>
Here's the output :
[
{
"id" : "1",
"value" : "5",
"checked" : false
},
{
"id" : "2",
"value" : "10",
"checked" : false
},
{
"id" : "3",
"value" : "15",
"checked" : false
},
{
"id" : "4",
"value" : "20",
"checked" : false
}
]
I would like to have the second JSONObject's checked to display true. I doubt whether the comparison for the checked item is causing this error.
Any code help would be appreciated
I've been trying to think of a way to search products by Name AND Producer, not just one of them. Looking through documentation, I found how to tie search filter to one kind, but not multiple (not all).
For example, if I have a list of persons with names, surnames and phone numbers, I'd like to be able to search through only name and surname in one input field. If I type in my search field, this model will look for a match in Name and Producer. How do I achieve this?
Thank you in advance!
My code above.
HTML:
<div>
<label>Search</label>
<input ng-model="query" type="text" placeholder="Search for name or producer" autofocus>
<label>
</div>
<table>
<tr>
<td>Name</td>
<td>Producer</td>
<td></td>
<td>Type</td>
<td>Packaging</td>
<td>Is it alcoholic?</td>
<td>Volume</td>
<td>Popularity</td>
<td>Country</td>
<td>Added</td>
<td>Price</td>
</tr>
<tr ng-repeat="item in products | filter:query | orderBy: drinkOrder:direction">
<td>{{item.name}}</td>
<td>{{item.producer}}</td>
<td><img ng-src="img/{{item.picture}}.jpg" alt="{{item.name}}" height="50px"></td>
<td>{{item.type}}</td>
<td>{{item.packaging}}</td>
<td>{{item.alko}}</td>
<td>{{item.volume}}</td>
<td>{{item.popularity}}</td>
<td>{{item.country}}</td>
<td>{{item.added}}</td>
<td>{{item.price}}</td>
</tr>
</table>
JS file app.js:
var myApp = angular.module('myApp', []);
myApp.controller('MyController', ['$scope', '$http', function ($scope, $http) {
$http.get('/lynda/ithouse/js/data.json').success(function(data) {
$scope.products = data;
$scope.drinkOrder = 'popularity';
});
}]);
JSON file:
[
{
"name" : "Sample Beer",
"producer" : "Great Beer Producer",
"picture" : "two",
"type" : "beer",
"packaging" : "glass",
"alko" : "yes",
"volume" : "1",
"popularity" : "1",
"country" : "Latvia",
"added" : "2015-01-03",
"price" : "20,40"
},{
"name" : "Sample Cider",
"producer" : "Great Cider Producer",
"picture" : "one",
"type" : "cider",
"packaging" : "plastic",
"alko" : "yes",
"volume" : "2",
"popularity" : "3",
"country" : "Estonia",
"added" : "2015-01-03",
"price" : "20,40"
},{
"name" : "Best Wine",
"producer" : "Wine for You",
"picture" : "eight",
"type" : "wine",
"packaging" : "glass",
"alko" : "yes",
"volume" : "2",
"popularity" : "5",
"country" : "Lithuania",
"added" : "2015-01-03",
"price" : "20,40"
},{
"name" : "Another Beer",
"producer" : "Beer Land",
"picture" : "seven",
"type" : "beer",
"packaging" : "aluminium",
"alko" : "no",
"volume" : "4",
"popularity" : "2",
"country" : "Latvia",
"added" : "2015-05-03",
"price" : "21,40"
}
]
You can create your own custom filter like below
Filter
.filter('customFilter', function() {
return function(query) {
var out = [];
angular.forEach($scope.products, function(value, key) {
if (value['name'] == query || value['producer'] == query)
this.out.push(value)
})
return out;
}
});
Fiddle Here
I have the following array:
var boofers = [
{
"id" : "kurfki8z6786",
"query" : {"name": "foo", "status": "open"},
"uid" : "1416422209395",
},
{
"id" : "kurfki8z6874",
"query" : {"name": "puff", "status": "closed"},
"uid" : "1416422209387",
},
{
"id" : "kurfki8z6123",
"query" : {"name": "joe", "status": "closed"},
"uid" : "1416422209301",
}
]
I need to print the uid values of boofers for the status of query in boofer objects to be closed, so I am trying the following but not getting the result as nothing gets printed for {{boofer.uid}}:
<div ng-repeat="boofer in boofers | filter: {query.status: 'closed'}">{{boofer.uid}}</div>
Could somebody tell me the mistake I am committing here and how to resolve it?
Remove the ) from each id in the JSON array:
boofers = [
{
"id" : "kurfki8z6786",
"query" : {"name": "foo", "status": "open"},
"uid" : "1416422209395",
},
{
"id" : "kurfki8z6874",
"query" : {"name": "puff", "status": "closed"},
"uid" : "1416422209387",
},
{
"id" : "kurfki8z6123",
"query" : {"name": "joe", "status": "closed"},
"uid" : "1416422209301",
}
];
here:
http://jsfiddle.net/nk5Loxoy/
NOTE: If you are using an array in the query as before the edit, you should use a custom filter
You could use ng-if?
<div ng-repeat="boofer in boofers" ng-if="boofer.query.status==='closed'">{{boofer.uid}}</div>
I have the following data scheme in mongodb database.
Due to a user interaction an entry can be moved from CAT_A to CAT_B, and the angularjs model changes appropriately.
[
{
"_id":"537f4407cb8a077d396bd73e",
"cat":"CAT_A",
"ntype":"category",
"entries":[
{
"title":"111",
"content":"Content One",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd741"
},
{
"title":"222",
"content":"Content Two",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd740"
},
{
"title":"333",
"content":"Content Three",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd73f"
}
]
},
{
"_id":"537f4407cb8a077d396bd742",
"cat":"CAT_B",
"ntype":"category",
"entries":[
{
"title":"444",
"content":"Content Four",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd745"
},
{
"title":"555",
"content":"Content Five",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd744"
},
{
"title":"666",
"content":"Content Six",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd743"
}
]
}
]
How do I save this new model to the mongo database, or really what is the best way to handle this?
Things I've though about doing:
Simply remove all the categories involved(there will be more than 2) from the database, and recreate them from the new model. This seems inefficient, also the content field may contain larger amounts of data, which makes http requests expensive.
Same as 1, but leave the 'content' out of the schema, create a new collection for content only, and somehow link that to the entry ID.
Pull an entry from CAT_A and push to CAT_B, have struggled getting this working and what if I wanted to keep the index position as in the model? ie if I wanted to move entry 6 in CAT_B to between entry 1 and 2 in CAT_A?
cheers
EDIT new schemas:
var CatSchema = new Schema({
name : String,
ntype : String,
incentries: {
ntype : String,
entry_id : { type: Schema.Types.ObjectId, ref: 'Entry' },
entry_title : String
}
});
var EntrySchema = new Schema({
cat : { type: Schema.ObjectId, ref: 'Cat' },
title : String,
content : String,
});
and the code:
exports.editCat = function (req, res) {
Cat.update({_id: req.body.old},
{$pull: {'incentries': {'entry_id': req.body.entry}}},
function (err, data) {
});
Cat.update({_id: req.body.new},
{$addToSet: { incentries : {'entry_id': req.body.entry, 'entry_title': req.body.entryTitle, ntype: 'entry' }}},
function (err, data) {
});
};
Use $pull and $push:
>db.elements.findOne()
{
"_id" : ObjectId("537f6ddcd66d3634fe5963f6"),
"arr" : [
{
"_id" : "537f4407cb8a077d396bd73e",
"cat" : "CAT_A",
"ntype" : "category",
"entries" : [
{
"title" : "111",
"content" : "Content One",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd741"
},
{
"title" : "222",
"content" : "Content Two",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd740"
},
{
"title" : "333",
"content" : "Content Three",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd73f"
}
]
},
{
"_id" : "537f4407cb8a077d396bd742",
"cat" : "CAT_B",
"ntype" : "category",
"entries" : [
{
"title" : "444",
"content" : "Content Four",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd745"
},
{
"title" : "555",
"content" : "Content Five",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd744"
},
{
"title" : "666",
"content" : "Content Six",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd743"
}
]
}
]
}
> db.elements.update({"_id" : ObjectId("537f6ddcd66d3634fe5963f6")},{$pull:{"arr.0.entries":entry},$push:{"arr.1.entries":entry}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>db.elements.findOne()
{
"_id" : ObjectId("537f6ddcd66d3634fe5963f6"),
"arr" : [
{
"_id" : "537f4407cb8a077d396bd73e",
"cat" : "CAT_A",
"ntype" : "category",
"entries" : [
{
"title" : "222",
"content" : "Content Two",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd740"
},
{
"title" : "333",
"content" : "Content Three",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd73f"
}
]
},
{
"_id" : "537f4407cb8a077d396bd742",
"cat" : "CAT_B",
"ntype" : "category",
"entries" : [
{
"title" : "444",
"content" : "Content Four",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd745"
},
{
"title" : "555",
"content" : "Content Five",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd744"
},
{
"title" : "666",
"content" : "Content Six",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd743"
},
{
"title" : "111",
"content" : "Content One",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd741"
}
]
}
]
}
But you will have to know where CAT_A and CAT_B are within the array (position 0 and position 1)
You could also try a schema like this:
{
"_id" : ObjectId("537f6dddd66d3634fe5963f7"),
"categories" : {
CAT_A: {
"_id" : "537f4407cb8a077d396bd73e",
"cat" : "CAT_A",
"ntype" : "category",
"entries" : [...]
},
CAT_B:{
"_id" : "537f4407cb8a077d396bd742",
"cat" : "CAT_B",
"ntype" : "category",
"entries" : [...]
}
}
}
So you update query would finally be:
> db.elements.update({"_id" : ObjectId("537f6ddcd66d3634fe5963f6")},{$pull:{"categories.CAT_B.entries":entry},$push:{"categories.CAT_A.entries":entry}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
As long as you "cat" field is unique within a single document this other approach seems better to me.