setting up ngDialog - angularjs

Guys I'm new to angular and trying to figure out how to implement a module called ngDialog so that I can have a pop up appear with angular data ect. Right now, I have my controller:
var oflApp = angular.module('oflApp', ['ngDialog']);
oflApp.controller('oflClassCtrl', function ($scope,ngDialog) {
$scope.classes = [
{"class": "ENGLISH I: INTRO TO LITERATURE AND COMPOSITION Sem 2", "href": "../courses/english.php#p1"}, ect
}];
$scope.clickToOpen = function(index){
$scope.selectedClass= $scope.classes[index];
ngDialog.open({template: '../inc/popUp.html'});
};
});
And html:
<table ng-controller="oflClassCtrl">
…
<tr ng-repeat = "selectedClass in classes | filter:searchTxt">
<td><a ng-click="clickToOpen($index)">{{selectedClass.class}}</a></td>
of course i've added the scripts underneath my angular scripts but is there anything else i need for this to work? I'm not sure what's going on but the pop will not show up on click so im sure something is wrong. What am I doing wrong? Thanks friends.
edit: I refactored my js because i was bored and read about doing it this way, but still doesn't work
(function(){
angular.module('oflApp', ['ngDialog'])
.controller('oflClassCtrl', oflClassCtrl)
function oflClassCtrl($scope,ngDialog) {
// $http.get('../courses/coursedata.json');
$scope.clickToOpen = function(index){
$scope.selectedClass= $scope.classes[index];
ngDialog.open({template: '../inc/popUp.html'});
};
$scope.classes = [
{"class": "ENGLISH I: INTRO TO LITERATURE AND COMPOSITION Sem 2", "href": "../courses/english.php#p1"},ect..
];
} //cntrler
})();

Related

How to access "Title" from this JSON object, using AngularJS

Cinemalytics API endpoint, which gives a JSON output:
http://api.cinemalytics.com/v1/movie/releasedthisweek?auth_token=[token]&callback=JSON_CALLBACK
How to access "Title" from this JSON object, using AngularJS
[
{
"Id": "b2a1844b",
"ImdbId": "tt0315642",
"OriginalTitle": "Wazir",
"Title": "Wazir",
"Description": "'Wazir' is a tale of two unlikely friends, a wheelchair-bound chess grandmaster and a brave ATS officer. Brought together by grief and a strange twist of fate, the two men decide to help each other win the biggest games of their lives. But there's a mysterious, dangerous opponent lurking in the shadows, who is all set to checkmate them.",
"TrailerLink": "https://www.youtube.com/watch?v=gdwM7xKOph0",
"TrailerEmbedCode": "<iframe width=\"850\" height=\"450\" src=\"https://www.youtube.com/embed/gdwM7xKOph0\" frameborder=\"0\" allowfullscreen></iframe>",
"Country": "IN",
"Region": "BOLLYWOOD",
"Genre": "Drama",
"RatingCount": 16,
"Rating": 3.7,
"CensorRating": "U/A",
"ReleaseDate": "1/8/2016",
"Runtime": 102,
"Budget": 0,
"Revenue": 0,
"PosterPath": "https://s3-ap-southeast-1.amazonaws.com/cinemalytics/movie/F6E428DA299F2ECEED5B4D3B1723A202.jpg"
},
{
"Id": "1e3424cb",
"ImdbId": "tt3777164",
"OriginalTitle": "Chauranga",
"Title": "Chauranga",
"Description": "A fourteen-year-old Dalit boy (Soham Maitra) is growing up in an unnamed corner of India. His dream is to go to a town school like his elder brother (Riddhi Sen) and his reality is to look after the pig that his family owns. His only escape is to sit atop a Jamun tree and adore his beloved (Ena Saha) passing by on her scooter. His unspoken love is as true as his mother’s helplessness who cleans the cowsheds of the local strongman’s mansion, with whom she also has a secret liaison. When the boy’s elder brother comes on a vacation to the village, he soon finds out about his younger brother’s infatuation. The learned elder brother makes him realize the need to express his love and helps him write a love letter.",
"TrailerLink": "https://www.youtube.com/watch?v=Nu50meFTNU4",
"TrailerEmbedCode": "<iframe width=\"850\" height=\"450\" src=\"https://www.youtube.com/embed/Nu50meFTNU4\" frameborder=\"0\" allowfullscreen></iframe>",
"Country": "IN",
"Region": "BOLLYWOOD",
"Genre": "Drama",
"RatingCount": 17,
"Rating": 3.84706,
"CensorRating": "U/A",
"ReleaseDate": "1/8/2016",
"Runtime": 90,
"Budget": 0,
"Revenue": 0,
"PosterPath": "https://s3-ap-southeast-1.amazonaws.com/cinemalytics/movie/083FFED0BC933C2D60E8891B89269EB3.jpg"
}
]
With your current URL
http://api.cinemalytics.com/v1/movie/releasedthisweek?auth_token=<auth_token>&callback=JSON_CALLBACK
you may have cross origin issues. However you can use the (https://crossorigin.me/) service to avoid this issue.
Then, you should request:
https://crossorigin.me/http://api.cinemalytics.com/v1/movie/releasedthisweek?auth_token=<auth_token>&callback=JSON_CALLBACK
Finally, this can be easily used in your AngularJS code.
I've made a demo, using an AngularJS Service and Controller.
(function() {
var app = angular.module("myApp", []);
// Service
app.service("Service", ["$http",
function($http) {
return {
getData: function() {
return $http.get("https://crossorigin.me/http://api.cinemalytics.com/v1/movie/releasedthisweek?auth_token=<auth_token>&callback=JSON_CALLBACK", null, {
responseType: "json"
});
}
};
}
]);
// Controller
app.controller("Controller", ["$scope", "Service",
function($scope, Service) {
$scope.data = [];
$scope.list = function() {
// Calling the getData() function from the Service.
Service.getData().then(function(response) {
$scope.data = response.data; // Store the response in the data variable.
}, function(response) {
console.log(response); // if there is an error...
});
};
$scope.list(); // Call to the function once.
}
]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div data-ng-app="myApp">
<div data-ng-controller="Controller">
<ul>
<!-- As you have an array, you should use data-ng-repeat to show the items collection. -->
<!-- Use data-ng-bind to show the Title (One-way data binding). -->
<li data-ng-repeat="item in data" data-ng-bind="item.Title">
</li>
</ul>
</div>
</div>
Hope this helps.
If the JSON was in a variable movies:
movies.map(function (m) {return m.Title;});
See JSbin example.
With querying the endpoint:
$http.get('http://api.cinemalytics.com/v1/movie/releasedthisweek?auth_token=59CD0710AD10858DD65FE815F0181603&callback=JSON_CALLBACK')
.then(function(res) {
$scope.titles = res.map(function (m) {return m.Title;});
});
$scope.titles would hold the extracted titles.
With no other context or code attempts that you have shown, here is what the simple Angular GET request would look like.
angular.module('myApp', [])
.controller('myController', function($http) {
var vm = this;
getObject();
function getObject() {
return $http.get('http://api.cinemalytics.com/v1/movie/releasedthisweek?auth_token=59CD0710AD10858DD65FE815F0181603&callback=JSON_CALLBACK')
.then(function(res) {
vm.object = res.data;
});
});
<div ng-app="myApp">
<div ng-controller="myController as vm" ng-repeat="item in vm.object track by item.Id">
Item {{item.Id}} - Title: {{item.Title}}
</div>
</div>

AngularJs Typeahead directive not working

I have a simple requirement wherein a list of users is displayed and display a search button on top to search for the users by name, something like a simplified LinkedIn Connections page.
My web app is developed on node.js but this one page has been developed on angular.js and for this search button, I have decided to use the typeahead directive. This is how the jade file looks like:
html(ng-app='geniuses')
head
title List All Geniuses!
link(href='//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css', rel='stylesheet')
script(src='http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js')
script(src="https://cdn.firebase.com/js/client/2.2.4/firebase.js")
script(src="https://cdn.firebase.com/libs/angularfire/1.1.2/angularfire.min.js")
script(src='js/listAllgeniuses.js')
body
div.container
div.page-header
h2 All Geniuses!
div(ng-app='geniuses',ng-controller='SearchAGenius')
input.form-control(placeholder='Genius',name='search-genius',ng-model="selected",typeahead="user for user in usersArr | filter:{'geniusid':$viewValue} | limitTo:8")
div(ng-app='geniuses',ng-controller='GetAllGeniuses')
ul
li(ng-repeat='user in users') {{ user.geniusid }}
The list of users are being fetched as an array from firebase. As you can see, the list of users is fetched using GetAllGeniuses controller and it works fine.. Here is the controller code:
(function (angular) {
var app = angular.module('geniuses', ["firebase"]);
app.controller('GetAllGeniuses', ["$scope", "$rootScope","$firebaseArray",
function($scope, $rootScope, $firebaseArray) {
var users = $firebaseArray(new Firebase("****));
$rootScope.usersArr = users;
$scope.users = users;
}
])
app.controller('SearchAGenius', ["$scope", "$rootScope",
function($scope, $rootScope) {
$scope.selected = '';
$scope.usersArr = $rootScope.usersArr;
}
])
}(angular));
This is how the data looks like(dummy):
[
{
geniusid: "new",
geniusname: ""
},
{
geniusid: "new",
geniusname: ""
},
{
geniusid: "news",
geniusname: ""
},
{
geniusid: "qazwsx",
geniusname: ""
}
]
I want to search using the geniusid (or name) in the search box... I have tried almost all ideas posted on the net but haven't been able to figure this out..
Any ideas would be appreciated.
Check out this Plunker I made using your demo.
A few things to note. You'll want to include Angular Bootstrap in your scripts and inject it into your module.
script(src='http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.min.js')
And
var app = angular.module('geniuses', ["firebase","ui.bootstrap"]);
Also, don't use $rootScope to pass data around. This is a prefect use for an angular service.
There's also no need to define ng-app everytime you're going to use angular.
Here's the rest of the plunker code that I modified to get this working.
html(ng-app='geniuses')
head
title List All Geniuses!
link(href='//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css', rel='stylesheet')
script(src='http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js')
script(src='http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.min.js')
script(src="https://cdn.firebase.com/js/client/2.2.4/firebase.js")
script(src="https://cdn.firebase.com/libs/angularfire/1.1.2/angularfire.min.js")
script(src="./app.js")
body
div.container
div.page-header
h2 All Geniuses!
div(ng-controller='SearchAGenius')
input.form-control(placeholder='Genius',name='search-genius',ng-model="selected",typeahead="user as user.geniusname for user in usersArr | filter:{'geniusid':$viewValue} | limitTo:8")
div(ng-controller='GetAllGeniuses')
ul
li(ng-repeat='user in users') {{ user.geniusid }}
And the JS
(function(angular) {
var app = angular.module('geniuses', ["firebase", "ui.bootstrap"]);
app.controller('GetAllGeniuses', ["$scope", 'GeniusFactory',
function($scope, GeniusFactory) {
$scope.users = GeniusFactory.users();
}
]);
app.controller('SearchAGenius', ["$scope", 'GeniusFactory',
function($scope, GeniusFactory) {
$scope.selected = '';
$scope.usersArr = GeniusFactory.users();
}
]);
app.factory('GeniusFactory', ["$firebaseArray", function($firebaseArray) {
//Create a users object
var _users;
return {
users: users
}
function users() {
//This will cache your users for as long as the application is running.
if (!_users) {
//_users = $firebaseArray(new Firebase("****"));
_users = [{
geniusid: "new",
geniusname: "Harry"
}, {
"geniusid": "new",
"geniusname": "Jean"
}, {
"geniusid": "news",
"geniusname": "Mike"
}, {
"geniusid": "qazwsx",
"geniusname": "Lynn"
}];
}
console.log(_users);
return _users;
}
}]);
})(angular);

AngularJS compile a template and use it in Showdown extension

I'm attempting to create a Showdown extension in my Angular app which will show scope variables. I was able to get it setup to show basic scope variables easily enough, but now I'd like to get it to where I can use the results of an ng-repeat and I can't get anything other than [[object HTMLUListElement]] to show.
Here's my controller so far:
app.controller('MyCtrl', ['$scope', '$window', '$compile', function($scope, $window, $compile){
$scope.machines = [
{ abbv: 'DNS', name: 'Did Not Supply' },
{ abbv: 'TDK', name: 'The Dark Knight' },
{ abbv: 'NGG', name: 'No Good Gofers'}
];
$scope.machine = $scope.machines[0];
$scope.machine_list = $compile('<ul><li ng-repeat="m in machines">{{m.abbv}}: {{m.name}}</li></ul>')($scope);
$scope.md = "{{ machine_list }}";
var scopevars = function(converter) {
return [
{ type: 'lang', regex: '{{(.+?)}}', replace: function(match, scope_var){
scope_var = scope_var.trim();
return $scope.$eval(scope_var);
}}
];
};
// Client-side export
$window.Showdown.extensions.scopevars = scopevars;
}]);
Plunkr: code so far
I feel like I've got to be close, but now I don't know if I'm on the completely wrong track for this, or if it's a showdown thing or an angular thing or what.
I realized I was fighting Angular (and losing quiet badly) with the way I was doing that. DOM in a controller is a no-no. And now I'm kind of angry about how easy it is once I started thinking properly and looking at the directive.
Now instead of trying to do the compile and everything within the controller, I included the $compile service in the directive I'm using, so:
htmlText = converter.makeHtml(val);
element.html(htmlText);
became:
htmlText = converter.makeHtml(val);
element.html(htmlText);
$compile(element.contents())(scope);
With that change in place I no longer need the portion of the extension that just did the basic evaluation, since it's being compiled {{ machine.name }} is automatically converted.
But that still left me not being able to specify a variable to insert a template, just variables. But now that the output is going to be compiled through angular I can put the template in a partial and use an extension to convert from a pattern to an ng-include which works.
New Controller Code:
app.controller('MyCtrl', ['$scope', '$window', '$compile', function($scope, $window, $compile){
$scope.machines = [
{ abbv: 'DNS', name: 'Did Not Supply' },
{ abbv: 'TDK', name: 'The Dark Knight' },
{ abbv: 'NGG', name: 'No Good Gofers'},
{ abbv: 'TotAN', name:'Tales of the Arabian Nights'}
];
$scope.machine = $scope.machines[0];
$scope.tpls = {
'machinelist': 'partials/ml.html'
};
$scope.md = "{{machines.length}}\n\n{{include machinelist}}";
var scopevars = function(converter) {
return [
{ type: 'lang', regex: '{{include(.+?)}}', replace: function(match, val){
val = val.trim();
if($scope.tpls[val] !== undefined){
return '<ng-include src="\''+$scope.tpls[val]+'\'"></ng-include>';
} else {
return '<pre class="no-tpl">no tpl named '+val+'</pre>';
}
}}
];
};
// Client-side export
$window.Showdown.extensions.scopevars = scopevars;
}]);
And of course the new plunkr
Hope this can help someone later down the road

PhoneGap: angularjs data appear in browser, not in mobile

Like the title say, i'am in the development of an hybrid app using Phonegap + Ionic. I have some angularjs code and the data appear perfectly in the browser but when i test the app in Genymotion (or in a real mobile) doesn't appear.
Here is the html part:
<div class="wod-content-training padding-h4">
<div ng-repeat="sport in training.exercises" class="training no-padding clear-fix roboto-l">
<div class="icon-full w-12" ng-class="sport.image"></div>
<div class="font-9pt relative flexcenter-h-v w-14">{{sport.count}}</div>
<div class="text-right font-9pt relative text-vcenter w-52">< {{sport.name}} ></div>
<div class="icon-full whiteblue-eye w-08"></div>
</div>
</div>
Tell me if you need something about the angularjs code but i think is not important at all. Becouse the code is working in the browser, so the problem is something about PhoneGap (or Cordova) maybe.
Here an image example that show how appear that html part in the browser:
And in the mobile is just empty.
Anybody know something about this? what can be the problem? Sorry if my english is not perfect.
Thanks in advance
UPDATE:
Here is the angularjs to understand how i get the sports part:
app.factory('trainingExercises', ['$q', '$timeout', '$http', function($q, $timeout, $http) {
var exercises = {
fetch: function() {
var deferred = $q.defer();
$timeout(function() {
$http.get('/resources/training.json').success(function(data) {
deferred.resolve(data);
});
}, 30);
return deferred.promise;
}
}
return exercises;
}]);
app.controller('freeWODController', ['$scope', '$location', 'trainingExercises', function($scope, $location, trainingExercises) {
$scope.pageTitle = 'WOD Libre';
trainingExercises.fetch().then(function(data) {
$scope.training = data;
});
}]);
and the training.json here:
{
"exercises": [
{
"name": "Overhead squat",
"image": "green-overhead-squat",
"count": "10"
},
{
"name": "Ring dip",
"image": "whiteblue-ring-dip",
"count": "10"
},
{
"name": "Toes to bar",
"image": "blue-toes-to-bar",
"count": "20"
},
{
"name": "Ring dip",
"image": "whiteblue-ring-dip",
"count": "10"
},
{
"name": "Toes to bar",
"image": "blue-toes-to-bar",
"count": "20"
}
]
}
UPDATE 2:
Thanks to jcesarmobile for recommend me GapDebug tool, which is really good for debugging apps by PhoneGap, and work with Genymotion too!
So now i have what is the problem, but don't know how to solve this at all. This line:
$http.get('/resources/training.json')
That is in www/js/app.js and the training.json is in www/resources/training.json so i tried:
$http.get('../resources/training.json')
But nothing. In the GapDebug i can see the FILE_NOT_FOUND for the training.json becouse the location path that is trying to get is: file:///android_asset/resources/training.json
How to properly set the location path in app.js?
Thanks!

Combine many array values to related array vlaue with Angularjs

I have been working with Angularjs for a few weeks now and am starting to LOVE it, but I have one last problem to solve that has alluded me...
Basically I have two json files with related values.. meaning there is one common key in each json file that I want to use to do what in SQL would be a join of sorts.. This is a one to many relationship. So one value related to multiple values. I'm close but I just can't seem to figure out how to say "for this array value, grab all of these related values from this other file then display them as one result".
I've also included Underscorejs and injected it into my controller as a possible way to solve this as well..
I'm not familiar enough yet to figure this out but here is my code so far:
Contents of the ab_activities.json (the file with the "many"):
[
{
"unique_id":"001",
"state":"NY",
"state_id":"1.S2.3a"
},
{
"unique_id":"001",
"state":"NY",
"state_id":"1.S3.2d"
},
{
"unique_id":"001",
"state":"FL",
"state_id":"SC.4.N.1.D"
}
]
Contents of the ee_activities.json file (the "one" related value):
[
{
"unique_id":"001",
"title":"Some Title",
"url_title":"some-title"
}
]
Angular code:
var underscore = angular.module('underscore', []);
underscore.factory('_', function() {
return window._;
});
var abApp = angular.module('abApp', ['underscore']);
abApp.factory('abData', function($http, $q) {
var deffered = $q.defer();
var data = [];
var abData = {};
abData.async = function() {
$http.get('/data/ab_activities.json')
.success(function(ab) {
data = ab;
deffered.resolve();
});
$http.get('/data/ee_activities.json')
.success(function(ee) {
data = ee;
//deffered.resolve();
});
return deffered.promise;
};
abData.data = function() {
return data;
};
return abData;
});
abApp.controller("abEE", function(abData, $scope) {
var abApp = this;
abData.async().then(function(d) {
abApp.subjects = abData.data();
});
})
And HTML:
<div ng-controller="abEE as ee">
<p ng-repeat="subject in ee.subjects">
<strong>{{subject.title}}</strong>
<ul>
<li ng-repeat="???">{{subject.state}}, {{subject.state_id}}</li>
</ul>
</p>
</div>
And finally - What I'm hoping to achieve:
Title: Some Title
NY, 1.S2.3a
NY, 1.S3.2d
FL, SC.4.N.1.D
The {{subject.state}} / {{subject.state_id}} are the many related values.. and I'm trying to relate the results by the "unique_id".
Right now the interesting thing that is happening is that there is one value from the ab_activities.json file that is being displayed multiple times because deffered.resolve(); is set on the ee_activities.json values and that is the one that has the "many to one" values.
I'm thinking that I can use a "forEach()" function then somehow pass the value that I want to relate back, then use underscore's ._where() function to get the related values?? Just not sure yet, hopefully someone will have some Angular code brilliance that can help out :-)
you are making it very complex through your existing code. its very simple to implement. I'm providing you fiddle. go through it carefully.
here is fiddle link
First of all, in this fiddle i'm using your json objects directly without making any server trip. so you have to manage, when you are modifying your code, on your own.
This will help you a lot to learn new thing...(its very easy)
I'm showing you dynamic way to bind data conditionally as per your requirement.
now if you have more data, angular will manage on its own...
html page.
<html ng-app>
<div ng-controller="ctrl">
<div ng-repeat="title in titles">
<div>{{title.title}}</div>
<div ng-repeat="state in states|filter:{unique_id:title.unique_id}">
<div >{{state.state}}{{state.state_id}}</div>
</div>
</div>
<div>
</html>
your controller .js file
function ctrl($scope) {
$scope.states = [
{
"unique_id":"001",
"state":"NY",
"state_id":"1.S2.3a"
},
{
"unique_id":"001",
"state":"NY",
"state_id":"1.S3.2d"
},
{
"unique_id":"001",
"state":"FL",
"state_id":"SC.4.N.1.D"
}
];
$scope.titles = [
{
"unique_id":"001",
"title":"Some Title",
"url_title":"some-title"
}
];
}
EDITED:
Updated fiddle according to your existing html page in here

Resources