I wrote a simple filter but it don't seem to work because of the variable I use. Here is the code:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
<script type="text/JavaScript">
var app = angular.module('myApp',[]);
app.factory('Property', function () {
/**
* Constructor, with class name
*/
function Property(newProperty) {
this.id = newProperty.id;
this.purchasePrice = newProperty.pprice;
this.marketValue = newProperty.mv;
this.stampDuty = newProperty.stamp;
this.sourcingFee = newProperty.sourcing;
this.legaFee = newProperty.legal;
this.otherFee = newProperty.other;
this.mortgage = null;
}
return {
createNew: function(newProperty) {
return new Property(newProperty);
},
setMortgage: function(newMortgage) {
console.log(newMortgage);
this.mortgage = Mortgage.createNew(newMortgage);
}
};
});
app.factory('portfolio', function(Property, $filter){
var portfolio = {};
portfolio.list = [];
portfolio.add = function(newProperty){
var prop = Property.createNew(newProperty);
portfolio.list.push(prop);
};
portfolio.getProperty = function(propertyId) {
var property = $filter('filter')(portfolio.list, propertyId, true);
alert("property found: " + $filter('json')(property) + $filter('json')(filterArg));
return property;
};
return portfolio;
});
app.controller('ListCtrl', function(portfolio) {
var self = this;
self.portfolio = portfolio.list;
});
app.controller('PostCtrl', function(portfolio){
var self = this;
self.addProperty = function(newProperty){
newProperty.id = portfolio.list.length;
portfolio.add(newProperty);
};
self.getProperty = function(search){
property = portfolio.getProperty(search);
}
});
</script>
</head>
<body ng-app="myApp">
<div style="text-align: center">
<h2>Initial investment</h2>
<ul class="list">
<input type="hidden" ng-model="newProperty.id" placeholder="id">
<input type="text" ng-model="search" placeholder="search">
<li class="list__item">
<input type="number" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.pprice" placeholder="Purchase price" required>
</li>
<li class="list__item">
<input type="number" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.mv" placeholder="Market value">
</li>
<li class="list__item">
<input type="number" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.stamp" placeholder="Stamp duty">
</li>
<li class="list__item">
<input type="number" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.sourcing" placeholder="Sourcing fee">
</li>
<li class="list__item">
<input type="number" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.legal" placeholder="Legal fee">
</li>
<li class="list__item">
<input type="number" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.other" placeholder="Other">
</li>
</ul>
</div>
<br />
<div ng-controller="ListCtrl as list">
<p ng-repeat="prop in list.portfolio track by $index">
New Object: {{prop.id}} - {{prop.purchasePrice}}: {{prop.legaFee}} </p>
</div>
<div ng-controller="PostCtrl as post">
<button class="button" ng-click="post.addProperty(newProperty)">Add Property</button><br/>
<button class="button" ng-click="menu.setMainPage('finance.html')">Next <ons-icon icon="ion-arrow-right-b"></ons-icon></button><br/>
<button class="button" ng-click="post.getProperty(search)">Search Property</button>
<p>{{newProperty}}</p>
</div>
</body>
</html>
So if we look closely at
portfolio.getProperty = function(propertyId) {
var property = $filter('filter')(portfolio.list, propertyId, true);
alert("property found: " + $filter('json')(property) + $filter('json')(filterArg));
return property;
};
It doesn't like it. If I change
var property = $filter('filter')(portfolio.list, {id: propertyId}, true);
for
var property = $filter('filter')(portfolio.list, {id: 2}, true);
it works (as long as I added 2 objects in portfolio.list)
I tried
var object = {};
object.id = propertyId;
var property = $filter('filter')(portfolio.list, object.id, true);
which doesn't work but this works
var object = {};
object.id = 1;
var property = $filter('filter')(portfolio.list, object.id, true);
and a few other things without success. What am I doing wrong ?