How to detect current state within directive - angular-ui

I'm using AngularUI's routing and I'd like to do a ng-class="{active: current.state}" but I'm unsure how to exactly detect the current state in a directive like this.

Update:
This answer was for a much older release of Ui-Router. For the more recent releases (0.2.5+), please use the helper directive ui-sref-active. Details here.
Original Answer:
Include the $state service in your controller. You can assign this service to a property on your scope.
An example:
$scope.$state = $state;
Then to get the current state in your templates:
$state.current.name
To check if a state is current active:
$state.includes('stateName');
This method returns true if the state is included, even if it's part of a nested state. If you were at a nested state, user.details, and you checked for $state.includes('user'), it'd return true.
In your class example, you'd do something like this:
ng-class="{active: $state.includes('stateName')}"

Also you can use ui-sref-active directive:
<ul>
<li ui-sref-active="active" class="item">
<a href ui-sref="app.user({user: 'bilbobaggins'})">#bilbobaggins</a>
</li>
<!-- ... -->
</ul>
Or filters:
"stateName" | isState & "stateName" | includedByState

If you are using ui-router, try $state.is();
You can use it like so:
$state.is('stateName');
Per the documentation:
$state.is ... similar to $state.includes, but only checks for the full state name.

The use of ui-sref-active directive that worked for me was:
<li ui-sref-active="{'active': 'admin'}">
<a ui-sref="admin.users">Administration Panel</a>
</li>
as found here under the comment labeled "tgrant59 commented on May 31, 2016".
I am using angular-ui-router v0.3.1.

Check out angular-ui, specifically, route checking:
http://angular-ui.github.io/ui-utils/

Related

Taghelpers get overwritten by ViewComponent

I have ASP.NET Core application (fka ASP.NET 5, ASP.NET MVC 6). The layout page has the following relevant links:
<li><a asp-controller="order">Orders</a></li>
<li><a asp-controller="customer">Customers</a></li>
<li><a asp-controller="whatever">Something Else</a></li>
and then (via ViewComponent)
<li><a asp-controller="account">Login</a></li>
Login.cshtml has the following:
<form asp-controller="Account" asp-action="Login" method="post" role="form">
...
</form>
or <form action="/Account/Login">...</form> - doesn't make any difference.
The problem is that once I click on Login and get into login screen, the top level links (Orders, Customers, etc.) all turn into "/Account/Login"! Obviously, if I put old-fashioned href, they remain unchanged. But the most puzzling part is that if I specify Action explicitly -
<li><a asp-controller="order" asp-action="Index">Orders</a></li>
Everything works correctly! Is it a bug that I stumbled upon, or is it by design that I don't understand?
The Anchor tag helper defaults to empty when it's unable to find the specified route, so it probably just defaulted to the current page.
<a asp-controller="SomeUnknownController">Link!</a>
Will produce
Link!
Since we don't know what's going on in your Account/Login action we cannot say for sure this is a bug in the anchor TagHelper. But something is messing with the routing somewhere.

Image Hyperlink in ASP.NET - MVC 4

I try to create a project for the first time in asp.net (mvc4).
and what i try to do is to create a image which is a hyperlink to go to the index page.
i have search a lot of things and it shows very simple to do that.
but i can´t understand why it doesn´t work for me.
someone can give a hand?
Code:
<a href="<%= Url.Action("Index","Home")%><img src="~/Content/imagens/nav-arrow-back.png"/></a>
The Action is "Index" in the controller calls Home.
you miss a quote
<a href="<%=Url.Action("Index","Home")%>"> ...
^
about this quote you missed
For bad request, fix the whole <img> part
<img src="<%=Url.Content("~/Content/imagens/nav-arrow-back.png")%>"/>
First up, as previously noted you're missing a closing quote on that href. Second, MVC 4 doesn't use the <% %> syntax, at least not by default; it should be using Razor v2 which uses #, so your code should look like this:
<img src="~/Content/imagens/nav-arrow-back.png"/>
If you use the old syntax I assume it would try to handle the actual text <%= Url.Action("Index","Home")%> as a URL, which clearly won't work.

Understanding Two-Way Data-Binding in AngularJS

I'm new to AngularJS. A long time I tried to abuse it the way I've always used Javascript-Frameworks like JQuery or Mootools. Now I understood that it's not gonna work like that anymore... But I've come across some big problems since I always generate my HTML-output with a CMS.
So it's pretty static, when it first comes out... Small example:
<ul>
<li>foo <span>delete</span></li>
<li>bar <span>delete</span></li>
<li>blub <span>delete</span></li>
</ul>
Now I thought, that Two-Way Databinding means I can generate the View with help of the Angular Scope and Controller, but also can generate Models by the View.
I may got something confused there... So here's my question. Is there any way to initiate Models from static HTML-output from a CMS?
I tried something like this...
<ul ng-controller="Ctrl">
<li ng-init="item[0].name=foo">{{item[0].name}} <span ng-click="remove(0)">delete</span></li>
<li ng-init="item[1].name=bar">{{item[1].name}} <span ng-click="remove(1)">delete</span></li>
<li ng-init="item[2].name=blub">{{item[2].name}} <span ng-click="remove(2)">delete</span></li>
</ul>
And in my controller I wrote a delete function. But when it did delete, it did only delete the name... the span-button was still there
It did work though when I defined my data as an javascript-array and did the whole output via Angular with ng-repeat... like this:
<ul ng-repeat="it in item">
<li>{{it.name}} <span ng-click="remove($index)">delete</span></li>
</ul>
I hope I made a point here and everyone get's my dificulty and problems? Can anyone tell me if what I was trying there is possible at all?
This is a common issue people have adjusting to Angular and other frameworks like it.
You don't need your server to render the HTML for you anymore. All you need to do is set up the template, and load the proper data into the scope.
<ul ng-controller="Ctrl" ng-init="getMyItems()">
<li ng-repeat="item in items">{{item.name}} <a ng-click="remove($index)">delete</a></li>
</ul>
And in your controller you'd do something like this
app.controller('Ctrl', function($scope, $http) {
$scope.items = [];
$scope.getMyItems = function(){
$http.get('/my/json/stuff/url').success(function(data) {
$scope.items = data;
$scope.$apply();
});
};
});
Now I know you're probably thinking "but I don't want to make a seperate request to get my JSON. And that's fine (probably irrelevant, but fine)... All you need to do is stick it into a global variable and retrieve it with $window instead.
Let's talk code. Here is real time app which shows profile pictures which is bind to data from MySQL. When anything changes in MySQL ( model ) view (HTML) will be updated.
app.controller('two_way_control',function($scope,$http,$interval){
load_pictures();
$interval(function(){
load_pictures();
},300);
function load_pictures(){
$http.get('http://localhost:3000/load').success(function(data){
$scope.profile_pictures=data;
});
};
Here is HTML
<div id="container" ng-app='two_way' ng-controller='two_way_control'>
<div class="row" ng-repeat="data in profile_pictures">
<div class=".col-sm-6 .col-md-5 .col-lg-6">
<h4>User Say's</h4><hr>
<p>
This is a Demo feed. It is developed to demonstrate Two way data binding.
</p>
<img src="{{data.profile_picture}}">
</div>
</div>
</div>
Learn more :
http://codeforgeek.com/2014/09/two-way-data-binding-angularjs/
Hope it helps !

How to avoid Adding runat="server" destroying my server tags <%...%>

Adding runat="server" is not rendering my server tags <%...%>
I have a masterpage with a few <li> for menu and since I have to set class=selected for the current page, I am using a little server tag to find the url and assign the particular class.
I have total of 10 <li> and not all menu is available to all types of user, I need to toggle few of the <li> if the user is not admin, so I have runat="server" added to them so I can set their visible=false through c#
Here is how it is at a glance:
<li runat="server" id="liBlog" class='<%= Request.Url.AbsoluteUri.EndsWith("/Blog") ? "selected" : "" %>'>Group Blog</li>
<li runat="server" id="liPoll" class='<%= Request.Url.AbsoluteUri.EndsWith("/Poll") ? "selected" : "" %>'>Poll</li>
<li id="liInvite" class='<%= Request.Url.AbsoluteUri.EndsWith("/Invite") ? "selected" : "" %>'>Invite</li>
<li id="liFavourite" class='<%= Request.Url.AbsoluteUri.Contains("/Favourite") ? "selected" : "" %>'>My Favourites</li>
The <li> without runat="server" works fine, when on correct page the source code shows class="selected" or class="" as appropriate, the other <li> used to work fine too, until I decided to add the runat="server".
Once I added that runat="server", the whole block of class="" is being sent out to the html page, its not processing the server tags at all! I right click on the html and look at the source, it's being rendered as:
<li id="ctl00_ctl00_ContentPlaceHolder1_liBlog" class="<%= Request.Url.AbsoluteUri.EndsWith("/Blog") ? "selected" : "" %>">Group Blog</li>
It's pouring out my server tags into the source code!
Why is this behaviour seen? How can I avoid it?
I looked up a lot of similar threads in here and there was nearly nothing in google, so made this, I dont think this is a duplicate question.
You can't use the <%= %> syntax inside the properties of tags that have the runat="server" attribute on them.
You either need to:
Set the properties via your code-behind
Create an Expression Builder (and part 2 and part 3) and use the <%$ %> syntax (note: these are links to stuff I wrote on my blog, so, beware the self link =)
for your requirement you can also use ASP.NET menu and XmlSiteMap to do the same thing.

How to WelCome control in Webpart code?

I need to place a webpart on the page. The webpart need to change Welcome control title. I need to change "WelCome UserName" to "UserName".
I tried http://www.wictorwilen.se/Post/Having-fun-with-the-SharePoint-Welcomeascx-control.aspx , But it did not worked in Webpart. If anybody have idea to change this control. Please share with me.
That article is a pretty comprehensive walkthrough of one way to do this - perhaps your focus should be to determine where you have gone wrong in the implementation of it?
An alternative (hacky) method would be to put some javscript into the page using a CEWP or modifying the master page that will search for the welcome string and remove it.
Hint - don't search for "Welcome", search for its container which looks a little like this (some attributes stripped out for clarity
<span title="Open Menu">
<div id="zz7_Menu_t" ...
class="ms-SPLink ms-SpLinkButtonInActive" >
<a accesskey="L" id="zz7_Menu" ... href="#" serverclientid="zz7_Menu"
menutokenvalues="MENUCLIENTID=zz7_Menu,
TEMPLATECLIENTID=zz3_ID_PersonalActionMenu">
Welcome UserName
</a>
</div>
</span>
Some links to get you started
http://www.dlocc.com/articles/style-a-page-using-the-content-editor-web-part-and-css/
http://www.cleverworkarounds.com/2008/02/28/more-sharepoint-branding-customisation-using-javascript-part-2/
http://blog.pathtosharepoint.com/2008/08/10/the-content-editor-web-part/

Resources