angular and ui-router - opening a detail page - angularjs

I'm sure ui-router is very useful when a site gets complex, but man is it indecipherable on a simple site.
I've got a table with a bunch of records on a summary page called /records/. (not concerned about how this page was built - it's built with ng-grid)
I want to click on record 102 and have it open /records/102 where I will use the id to show deets of 102.
This plunker is never going to work, but I've tried to put the basics in:
http://plnkr.co/edit/qfEaMM8Ihgg8gCUAMHbE?p=catalogue
enter code here
This: {{row.entity['id']}} does give me the record ID I'm looking for - I've got that working. It's a matter of how I get it to routes.js where it says url: '/records/:recId' and then how I get it to appear in the URL when it goes to the new page.
How do I do that?
(P.S. 'Submit Error: links to Plunker must be accompanied by code'. What??)

To redirect to /records/102 you just use a normal html anchor like this:
<a href="/records/{{row.entity['id']}}" ...>
Yo get the value out in your RecordsController you need to use the $routeParams service, inject it and use it like this:
$scope.recordId = $routeParams.recId;

Related

Pretty url with Angularjs/.htaccess

I've been struggling with this for a week now. I use Anguarjs and I set the html5mode(true) to get rid of the hash sign "#" that you needed in the url before.
Everything works fine amd my URL logic works with Query Parameters.
www.domain.com/thecurrentpage/?title=banana
And it changes and refresh the page when I click on something else and then it displays the right content.
I just want to be able to have a link like:
www.domain.com/thecurrentpage/banana
And come to the same page.
That actually will point you to the first one with query params.
I feels like I've tried everything in the .htaccess file with rewrite rules and everything with angular routing. I just don't get it.
you want to use the : syntax in your routing.
In your routing add a route like this:
/thecurrentpage/:title
Then you can use:
$routeParams.title
To get the value "banana"
This might help ... AngularJS: Read route param from within controller

Phonegap/Onsen/AngularJS Remove Page after Pagechange

I´m developing an App with Cordova Onsen-UI and AngularJS.
I have several pages in my application.
I change the page with that command:
$scope.ons.navigator.pushPage('msg_det.html', { animation: 'slide' });
I´ve noticed the following:
If I call the same page with the same command again, the old page still exist.
(I have a html element
for example: <img id="test" src="sadf.png" />
with an ID. At the first time, the access by id works.
$("#test").css("left", "20px");
After the second time it is not been working anymore.)
That is a sign, that the id still exists a second time.
Is there a chance to build the page "msg_det" new and delete the old one.
It seems that you need to check it manually
$scope.ons.navigator.getPages() gives you all the pages that currently exist, so check if your page already exist call the destroy() on old page.
for more info take a look at http://onsen.io/guide/overview.html#PageNavigation

Linking to external URL with different domain from within an angularJS partial

All I am trying to do is include an anchor tag inside the html of a partial that links to an external site. Were this standard html, the code would simply be:
google
As simple as this is, I cannot seem to find a working solution for getting past angular intercepting the route (or perhaps replacing my anchor with the https://docs.angularjs.org/api/ng/directive/a directive unintentionally?).
I have scoured SO and the rest of the web and seen a myriad of solutions for dealing with: links within the same domain, routing within the SPA, routing within a page (ala $anchorScroll) but none of these are my issue exactly.
I suspect it may having something to do with using $sce but I am an Angular n00b and not really sure how to properly use that service. I tried the following in my view controller:
$scope.trustUrl = function(url) {
return $sce.trustAsResourceUrl(url);
}
with the corresponding:
<a ng-href="{{ trustUrl(item) }}">Click me!</a>
(as described here: Binding external URL in angularjs template)
but that did not seem to do the trick (I ended up with just href="{{" in the rendered page).
Using a plain vanilla anchor link like this:
google
also failed to do the trick (even though some online advised that standard href would cause a complete page reload in angular: AngularJS - How can I do a redirect with a full page load?).
I also tried adding the target=_self" attribute but that seemed to have no effect either.
Do I need to write a custom directive as described here?
Conditionally add target="_blank" to links with Angular JS
This all seems way too complicated for such a simple action and I feel like I am missing something obvious in my n00bishness, at least I hope so because this process is feeling very onerous just to link to another url.
Thanks in advance for any solutions, advice, refs or direction.
It turns out that I did in fact have all anchor links in the page bound to an event listener and being overridden. Since that code was fundamental to the way the page worked I did not want to mess with it. Instead I bypassed it by using ng-click to call the new url as follows:
HTML:
<a class="navLinkHcp" href="{{hcpurl}}" title="Habitat Conservation Plan" target="_blank" ng-click="linkModelFunc(hcpurl)">Habitat Conservation Plan</a>
Controller:
$scope.hcpurl = 'http://eahcp.org/index.php/about_eahcp/covered_species';
$scope.linkModelFunc = function (url){
console.log('link model function');
$window.open(url);
}
And voila! Good to go.
Thanks again to KevinB for cluing me in that this was probably the issue.

AngularJS set title and description in head when populating content from JSON

Before I go and do this in Jquery out of frustration I figured I would ask what the angular way is?
I'm building an AngularJS site using a model based of the Phonecat tutorial example on the AngularJS site.
I found this method to set the title of a page and can work out how to modify it to do description as well in the app config but this doesn't work when I'm populating pages with content via json. I tried doing it using a ngbind method as well but have yet to find a working solution as I think something to do with the order in which files are loaded is breaking.
For example
when('/faq', {
templateUrl: 'sub_pages/articles.html',
title: 'Landing page title goes here, not to big a deal'
}).
when('/things-to-do/:activityID', {
templateUrl: 'sub_pages/activity-detail.html',
controller: 'activityDetailCtrl',
title: 'If I put a title here it will be the same on all of these pieces of content'
}).
What method can I use in order to set title on both the landing pages and also the pages which draw their content from a JSON feed?
EDIT - ANSWER BELOW
After about 2 days of bashing my head against a wall trying to work this one out it's actually quite simple and works for both static pages and templates with dynamically loaded content.
Inside the view pages (html that loads inside of ng-view) add a couple of divs (you can put this anywhere really) and then inside them you need to load in ng-init.
ng-init="$root.title = path.to.title"
ng-init="$root.description = path.to.description"
This will set the title and description on the root scope. The "path.to." is just an example path to content in json, you can replace this with plain text as well which is how I deal with landing pages.
Then on the index.html or what ever page your app is based on inside the head you just need to load in.
Your Page Title
This will automatically set your page title and description meta tags and you can pretty much use this formula for any other meta data you need to create.
I haven't tested this yet with Prerender.io or any other cache service but will do some checks and post the results here.
Something like
$document[0].title = "xyz";

Do we need multiple controllers to implement routes in angularjs?

There is chance that I might not be able to explain my problem properly. Let me try.
I am developing a single page application using angular. This app basically displays the episodes of an online novel series. There is a navigation bar, which has query menus (Like, latest episode, episode of a particular date, episodes with a particular tag, etc). For each of these queries, i want a separate url.
/latest - should display the latest episode
/tag/:tagname - should return all episodes with that tag.
For all these queries, the resultant view is the same (list of episodes). So I will be using the same partial for all routes.
My question is, Should I actually create a new controller for each query? like, LatestEpisodeController, TagController?
Is there anyway I can use the url to determine what the user wants and run that query from within the same controller?
Ofcourse you can use same controller in routing definition, the question is what is the purpose of that? It will be worse to debug it later, if you have a shared functionality it's better to turn it into a factory or service and then use in controllers.
But the answer is YES, you can use same controllers and implement different behaviour basing on i.e. $location.path()
yes you can use single controller for multiple routing..
you can create different functions in controller and in each function do the according job.
In my case I have created different html page for different url and registered same controller for the html pages and in the html page I have called controller method using ng-init in div portion.
You can use same controller and same views as you wish...
$location can help you to get current path or full url if you want and you can call your service depends on your path...
here I write a little example for you to get the idea
PLUNKER

Resources