Add or Remove ng-controller - html

My problem is i need to execute multiple times one controller, but i need to control that action. How can i add or remove an ng-controller from html tag. For example i have controller with name view and i need an <div ng-controller="view"> but i need a method to toggle it to <div>. On toggle would be better to delete the controller that took place, but i think angular do that automatically Any idea, perhaps something with directives?

You should use a directive instead of a controller. Controllers are not meant to be set and removed like this, the only way to do so would be to recompile the HTML each time which would bring more problems than it solves.
In short, this is an XY problem, please try to step back and formulate what problem exactly you are trying to resolve.

Related

CakePHP - Adding a "back to top" button on multiple pages

I have never worked with CakePHP before and need to add a button that'll take a user back to the top of a page. My question is how can this be done for multiple, but not all of pages, using the framework?
I couldn't figure it out and need a quick solution to this. Any help is appreciated!
Many ways to go about this one. Simplest is edit your default.ctp (or whichever) layout with a simple condition:
<?php if (!empty($backToTop)) : ?>
<!-- html & JS back to top code here-->
<?php endif; ?>
Then in the Controller Actions that you want the back to top button to display:
$this->set('backToTop',true);
If there are only a few exceptions to not having the 'back to top' script, then you may want to have the condition to exclude the code rather
Create a helper or a method in a generic utility helper for your app that renders the button or use an element that you can include where you need it.
If you want to call it on every page put your helper or element call in your layout file(s).
In the case you need an exception on a certain page you could put that logic in the helper method as well, or the element, and check if the request object attached to the view matches your desired controller and action.
Official Documentation:
Elements
Helpers
Layouts

Writing clean code in html

I want to validate a form and show error message corresponding to a validation code.
Following is my current code
<div class="alert alert-danger" ng-show="Ctrl.getValidationCode()=== Ctrl.VALIDATION_CODE['EXCEEDED_RANGE'] ">fromdate exceeded todate</div>
Is it better to use this
ng-show="Ctrl.getValidationCode()=== Ctrl.VALIDATION_CODE['EXCEEDED_RANGE']"
Or use this instead
ng-show="Ctrl.getValidationCode()=== 1"
I think the first one is better Because It's cleaner to the second one.
But the first one is too long.
Could you give me your opinions and advice?
First one is more practical, because you can give your code some freedom. So if you use that validation code on few pages, it's lot easier to manage that code. Because if you need to change code, you will change it only on one place instead of going to separate pages. It's something like convention.
I think that the best way is a new directive that handle all form erros messages in your application.

Generating a link to a controller action in Play Framework 2.3

I'm working on a Play application and need to generate links in a mixed Scala-HTML view that call controller actions. I found this question from a couple years ago that's similar to my situation, but the provided answers don't work for me.
The elements are generated in a loop so I can't manually insert the argument to the controller action, but nothing I've tried has worked. This is the line I have now:
ID: #{var fhirID = <processing for ID>; <a href='#routes.Users.fhirUserDetails(fhirID)'>fhirID</a>}
The accepted answer to the question I linked earlier effectively uses this structure too:
<a href='#routes.Application.show("some")'>My link with some string</a>
My issue here is twofold:
1) How can I have the variable fhirID passed to the controller action? My generated link simply has the text "fhirID" instead of what's generated by the first part of the statement.
2) Is the #routes.Users syntax correct? When I click the generated link, it literally attempts to render a page at /myapp/#routes.Users.fhirUserDetails(fhirID)
I realize I'm probably missing something very basic here- thanks for any advice!
The problem seems to be not the #routes syntax (which you have completely correct) but rather a case of the Twirl engine not seeing where code ends and HTML begins (or something like that anyway...)
The line you've included, which has both a var and a semicolon, made me suspect this, and I've been able to reproduce the problem when I use that style.
My recommendation is to use the #defining helper rather than var to get a scoped variable for use in your links, as follows:
ID: #defining(<processing for ID>) { fhirID =>
<a href='#routes.Users.fhirUserDetails(fhirID)'>fhirID</a>
}
You can nest #defining blocks as deeply as you like if necessary, although it's probably better to make a call out to a reusable block if there's a lot of logic. I think this style makes for more-readable templates and also somehow looks more like "real Scala" :-)

How to correctly mix html and Session data to form an html element

I have the following anchor element:
<a href="/cabinet/inbox/1">
That 1 in the url is supposed to be dynamic and be taken from Session data. So I need to mix it somewhat like this:
<a href="/cabinet/inbox/"#(String)Session["officerID"]
But I don't know how exactly this should be done. And besides, I also need to check if for some reason the Session["officerID"] is null like this:
<a href="/cabinet/inbox/"#Session["officerID"]!=null?"0":(string)Session["officerID"]
But it seems as I need to put # multiple times because in certain cases intellisense does not suggest the C# code.
In fact, I wanted to retrieve that value from ViewModel by strongly typing the view to System.String. But the current view is the master page for another view, which is strongly typed to a custom class. And it seems to be impossible for master page to have different view model than that of the slave one. So I decided to go the Session way.
UPDATE
To overcome the problem of having to write # multiple times I just need to to wrap the whole expression in brackets like this:
<a href="/cabinet/inbox/"#((String)Session["officerID"])
Don't use Session in a view. Use ViewBag, like so:
where are some point in your controller you've something like this line of code:
ViewBag.OfficerID = 42;
ViewBag is a dynamic object that will be evaluated at runtime. See http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase.viewbag%28v=vs.118%29.aspx for specifics.

Upload Image not using erb (form) rails

I was wondering if there is a way to upload images in rails 4 not using erb. I have my html code in quotations in my model (so that i can use it as a default for when creating a page). SO I cant put erb in the quotes, cuz if i do it'll come out as the erb code and not the actual view. (i.e. the upload image form. itll come out as the actual f.image_tag instead of the actual upload button).
So I am just seeing if there another way i could implement image uploading into the quotations that wont require erb. But, uses Rails 4.
If you have plain HTML code inside the string, and just want it to be displayed, you need to call .html_safe in your view, like:
'<div id="foo">bar</div>'.html_safe()
Note that normally is a bad practice: that doesn't mean it is always bad, but it means that it is always an attention point to make sure it is the best way to do it.
If you want to do image upload without using form.image_tag, you can just put that in your view and see the generated code (Ctrl+U in Chrome), that, in this case, will be something like:
<input type="file" id="something" />
And add that to the string. Probably some finer validation and options are added too via JS, that would be a pain to add this way - one way to be to add a script tag to the html with the code.
If, however, your string already contains code like this, and you are asking way it is not working, then you need to add an eval to the caller, e.g.:
eval('form.image_tag :image')
Not that this is an even worse smell, and it should be avoided at all costs.
Maybe it would be better to add some flags in your section.rb model, and then use that in a helper or in the view render the html; for example, a has_upload? field, and then you can do something like this:
if #section.has_upload?
form.image_tag :imagem
end