Expression showing as a json format. firebase - angularjs

I'm trying to show the data from my database using ng-repeat. However when I try to retrieve the equipment data under system. It shows it as a json format. How do i retrieve the other nodes under equipments? Need help.
My Angular
/*global angular*/
var app = angular.module('input', ['ngRoute', 'firebase']);
app.config(['$routeProvider', function ($routeProvider) {
'use strict';
$routeProvider.when('/input', {
templateUrl: 'input/input.html',
controller: 'inputCtrl'
});
}]);
app.controller('inputCtrl', ['$scope', '$firebaseObject', '$firebaseArray', function ($scope, $firebaseObject, $firebaseArray) {
'use strict';
$scope.message;
$scope.writeUserData = function (equipment, system) {
firebase.database().ref('data/' + $scope.system + '/equipments').child(equipment).set({
equipment: equipment
});
$scope.message = "Added"
};
var ref = firebase.database().ref();
var data = ref.child("data");
var list = $firebaseArray(data);
list.$loaded().then(function(data) {
$scope.data = data;
console.log($scope.data);
}).catch(function(error) {
$scope.error = error;
});
}]);
My Html
<tr data-ng-repeat="data in data">
<td class="select" align="center">
<div id="dd" class="wrapper-dropdown-3" tabindex="1">
<span>{{data.$id}}</span>
<ul class="dropdown">
<li>{{data.equipments}}</li>
</ul>
</div>
</td>
</tr>

have your tryed to change
data in data
to some thing like
d in data
also to iterate equipments you will need one more ng-repeat
<li ng-repeat= "equipment in d.equipments">{{data.equipments}}</li>
havent tested the code!

Alright I found a solution to my problem.
<tr data-ng-repeat="d in data">
<td class="select" align="center">
<div id="dd" class="wrapper-dropdown-3" tabindex="1">
<span>{{d.$id}}</span>
<ul class="dropdown">
<li data-ng-repeat="equipment in d.equipments"><a>{{equipment.equipment}}</a></li>
</ul>
</div>
</td>
</tr>

Related

ng-repeat Return empty rows

I tried with below code, but it is returning an empty row (more than 30+) of the table.
json
"[{\"COMPANY_ID\":\"1\",\"COMPANY_DESC\":\"11\",\"COMPANY_CURRENCY\":\"1\",\"ACTIVE\":true,\"IS_HEADOFFICE\":true,\"ACCOUNTING_SYSTEM_ID\":\"1\"},{\"COMPANY_ID\":\"2\",\"COMPANY_DESC\":\"2\",\"COMPANY_CURRENCY\":\"22\",\"ACTIVE\":false,\"IS_HEADOFFICE\":false,\"ACCOUNTING_SYSTEM_ID\":\"1\"}]"
Html
<tr ng-repeat = "company in companies track by $index"">
<td>{{ company.COMPANY_ID }}</td>
<td>{{ company.COMPANY_DESC }}</td>
<td>{{ company.COMPANY_CURRENCY }}</td>
</tr>
App
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://localhost:52087/api/accountmapping")
.success(function (response){
$scope.companies = angular.fromJson(response);
console.log(angular.fromJson(response));
});
});
</script>
use then instead of success. success is deprecated since angular version 1.4
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://localhost:52087/api/accountmapping")
.then(function (response){
$scope.companies = response.data;
console.log($scope.companies);
});
});
I try to mirror your codes but i don't find that error, please check my codes and tell me what i miss to show that error for me.
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
var myJsonAsText = '[{\"COMPANY_ID\":\"1\",\"COMPANY_DESC\":\"11\",\"COMPANY_CURRENCY\":\"1\",\"ACTIVE\":true,\"IS_HEADOFFICE\":true,\"ACCOUNTING_SYSTEM_ID\":\"1\"}, {\"COMPANY_ID\":\"2\",\"COMPANY_DESC\":\"2\",\"COMPANY_CURRENCY\":\"22\",\"ACTIVE\":false,\"IS_HEADOFFICE\":false,\"ACCOUNTING_SYSTEM_ID\":\"1\"}]';
var convertToJson = angular.fromJson(myJsonAsText);
console.log(convertToJson);
$scope.companies = convertToJson;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
<li ng-repeat = "company in companies track by $index"">
<p>COMPANY_ID: {{ company.COMPANY_ID }}</p>
<p>COMPANY_DESC: {{ company.COMPANY_DESC }}</p>
<p>COMPANY_CURRENCY: {{ company.COMPANY_CURRENCY }}</p>
<hr>
</li>
</ul>
</div>
I have updated the version of Angular. The problem has solved.
https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js

Angular why doesn't work nested ng-repeat

I use nested ng-repeat to loop in all objects values.
<tr class="" ng-repeat="(position, values) in chartResult.realData track by $index">
<td>
<div ng-click="select(position)" ng-class="{selected: position===selectedItem}">
<a href ng-click="select(position)">
<span>{{position}}</span>
</a>
</div>
</td>
<td ng-repeat="val in values">
{{val}}
</td>
</tr>
Why nested loop doesn't work?
Updated
app.controller('Ctrl',['$log', '$rootScope', '$scope', '$http', '$timeout',
function ($log, $rootScope, $scope, $http, $timeout) {
$http({
//request
}).then(
function successCallback(response) {
//after parse response realData view something like this
var res = {realData:
name1:[1,2,4,0,1,23],
name2:[4,6,3,7,2,3],
...
}
$scope.chartResult = res;
}
}]);
After add track by $index to nested loop it work as well.
If you pasted it correctly realData is not an array.
But please show us what is in the realData.

html does not have access to object in controller angularjs

I want to show the object in view. The object itself is the controller, but the html does not have access to its properties (probably not see the model)! Maybe the problem ui routing?
app.js
(function () {
'use strict';
angular
.module('hawk', [
'ngWebsocket',
'ui.bootstrap',
'ui.router',
'hawk.controllers',
'hawk.services',
'hawk.directives'
])
.config(['$stateProvider', '$urlRouterProvider', router])
.run(['$rootScope', main]);
angular.module('hawk.services', []);
angular.module('hawk.directives', []);
angular.module('hawk.controllers', []);
function router($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/list');
// CARDS OBJECT VIEW
$stateProvider
.state('list', {
abstract: true,
url: '/list',
templateUrl: '/app/app-eng/controllers/list.html',
controller: 'ListController as dc'
})
.state('list.cards-list', {
url: '/cards-list',
templateUrl: '/app/app-eng/controllers/object-card/cards-list.html',
controller: 'CardsListController as dc',
})
.state('list.contract', {
url: '/contract',
templateUrl: '/app/app-eng/controllers/object-card/contract.html',
controller: 'ContractController as dc',
})
}
function main ($rootScope) {
$rootScope.object = {};
}
})();
The submittion list.cards-list I have access to the object (from model), but submitting list.contract I get the object and can not access its properties (in model). Why?
list.html
<div class="list-group col-md-2 sidebar-offcanvas">
<uib-tabset active="activePill" vertical="true" type="pills">
<uib-tab index="0" heading="Cards list" ui-sref="list.cards-list"></uib-tab>
<uib-tab index="1" heading="Contract" ui-sref="list.contract"></uib-tab>
</uib-tabset>
</div>
<div class="col-md-10">
<div ui-view></div>
</div>
contract.html
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Contract #{{ 2222222}}</h3>
</div>
<table class="table table-condensed">
<tr>
<td width="10%" align="right"><strong>№ contract:</strong></td>
<td>{{dc.data.id}}</td>
</tr>
<tr>
<td align="right"><strong>Date start:</strong></td>
<td>{{dc.data.cdstart}}</td>
</tr>
<tr>
<td align="right"><strong>Date end:</strong></td>
<td>{{dc.data.cdend}}</td>
</tr>
<tr>
<td align="right"><strong>Type name:</strong></td>
<td>{{dc.data.tyonames}}</td>
</tr>
<tr>
<td align="right"><strong>Category.:</strong></td>
<td>{{dc.data.ccategory}}</td>
</tr>
<tr>
<td align="right"><strong>Police department:</strong></td>
<td>{{dc.data.rpnames}}</td>
</tr>
</table>
</div>
contract.js
(function() {
'use strict';
angular
.module('hawk.controllers')
.controller('ContractController', ContractController);
ContractController.$inject = ['$scope', 'Websocket'];
function ContractController ($scope, Websocket) {
var vm = this;
vm.data = {};
init();
function getContracts (id) {
console.log('getContracts-id', id);
Websocket.getContracts({ id: id }).then(function(data) {
console.log('getContracts-data', data);
vm.data = data.data;
console.log('getContracts-vm.data', vm.data);
});
}
function getAddress (id) {
Websocket.getAddress({ id: id }).then(function(data) {
console.log('getAddress', data);
vm.address = data.data;
});
}
function init () {
$scope.$watch('object.id', function(newValue, oldValue) {
console.log('cc', newValue, oldValue, $scope.object.id);
getContracts($scope.object.id);
});
}
}
})();
you have to access your variables with the "vm." inside your html -> "{{vm.data}}" etc.
//EDIT:
If you want to access them without the "vm" you have to put your variables into the "$scope" in your controller.

I tried to pass Json data to view using angular js

Just im trying to pass my data using Angular JS but im getting blank page
i choose this example http://www.w3schools.com/angular/tryit.asp?filename=try_ng_customers_sql
Then i tried from my local database like this
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>#{{ x.id }}</td>
<td>#{{ x.name }}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://localhost/sample/public/foo")
.then(function (response) {
$scope.names = response.data.hotels;
});
});
</script>
My http://localhost/sample/public/foo out put is like below so what im doing wrong
"{"hotels":[{"id":1,"name":"Sanmira Renaissance"},{"id":2,"name":"Galadari Hotel"},{"id":3,"name":"Grand Oriental Hotel"},{"id":4,"name":"Fullmoon Garden Hotel"},{"id":5,"name":"Ramada Hotel \u2013 Colombo"},{"id":6,"name":"Taj Samudra"},{"id":7,"name":"Cinnamon Grand"},{"id":8,"name":"Cinnamon Lakeside"}]}"
There is a problem with your JSON which is not formated correctly, and change your controller like this,
app.controller("listController", ["$scope","$http",
function($scope, $http) {
$http.get('test.json').then(function (response){
$scope.hotels = response.data.hotels;
});
}]);
Working application

How do I search JSON data originating on another server using AngularJS

I have data to search through. I am writing a directory app in AngularJS that takes JSON data from one of our servers; an employee list. I need to be able to do a live search on that data, but nothing I have tried seems to work. Specifically, the view does not update.
Code: app.js
var app = angular.module('directory', ['ngRoute']);
app.service('DirectoryService', ['$http', function($http){
var baseURL = "http://xxx.xxx.xxx.xxx/accounts/people/json";
return{
getEmployees: function() {
return $http.get(baseURL).then(function(response){
return response.data;
});
}
}
}]);
app.config(['$routeProvider', function($routeProvider){
$routeProvider
.when('/employees', {
templateUrl: '_common/partials/list.html'
})
.otherwise({
redirectTo: '/employees'
});
}]);
app.controller('DirectoryController', ['$scope', 'DirectoryService',
function($scope, DirectoryService){
$scope.searchName = '';
DirectoryService.getEmployees().then(function(data){
$scope.employeeList = data;
console.log(data);
});
}]);
Code: list.html
<div id="searchSection">
<input id="searchValue" type="text" placeholder="Search by Name" ng-
model="searchName" />
</div>
<div id="listView">
<ul>
<li ng-repeat='employee in employeeList | filter: {name:searchName}'>
<p>{{employee.name}}</p>
</li>
</ul>
</div>
When I've added filters I simple add an input with ng-model:
<input class="form-control" ng-model="searchFilter" placeholder="Filter..." />
Then in my ng-repeat:
<tbody ng-repeat="user in usc.users | filter:searchFilter">
<tr>
{{user.name}}
</tr>
</tbody>
That should filter through all the names.
Better to use a factory for your service, have the service hold the data for you, and reference it in your controller. Something along these lines should work:
app.factory('DirectoryService', ['$http', function($http) {
var service = {};
service.data = {
baseURL:"http://xxx.xxx.xxx.xxx/accounts/people/json",
employeeList:[],
searchName:""
}
service.getEmployees = function() {
$http.get(service.data.baseURL).success(function(response) {
service.data.employeeList = response;
})
}
return service;
}])
Then your controller function can reference the service's data, which will dynamically update:
app.controller('DirectoryController', ['$scope', "DirectoryService", function($scope, DirectoryService) {
$scope.ds = DirectoryService.data;
DirectoryService.getEmployees();
}])
So your relevant HTML function should look like this:
<input id="searchValue" type="text" placeholder="Search by Name" ng-model="ds.searchName" />
<li ng-repeat="employee in ds.employeeList | filter:{name:ds.searchName}">
<p>{{employee.name}}</p>
</li>
So while I was trying your problem on fiddle I came across a problem and that is ng-model="searchName" is not binding to the model value.
And the reason was there was a space between
ng-[space]model. //also ng-model should be in red in your code above but it is in red and black..:P
So it might be possible that this could be the issue with this.
One more this do not use any other parameter with filter like you did here
use this simple form.
<li ng-repeat="employee in ds.employeeList | filter : searchName">
Here is my fiddle with the working solution Fiddle Solution
<div ng-app="directory" id="searchSection" ng-controller="DirectoryController">
<input id="searchValue" type="text" ng-model="searchName" />
<div id="listView">
<ul>
{{searchName}}
<li ng-repeat="employee in employeeList | filter: searchName">
<p>{{employee.name}}</p>
</li>
</ul>
</div>
</div>
js:
var app = angular.module('directory', []);
app.controller('DirectoryController', ['$scope',
function($scope){
$scope.searchName = 'Fi';
$scope.employeeList = [{name : "First Employee", salary : 100}, {name : "Second Employee", salary : 200}];
}]);
You can not because the mechanism json is limited to the domain you belong. To do this you must use the jsonp

Resources