Building on my previous post about creating alert dialogs with AngularJS, here we will look at creating confirmation dialogs we can use to get confirmation from the user before completing a destructive action such as deleting something.
In the directive we add a new scope variable 'confirmPositive' with the '&' binding to allow our controller to assign a function to it. When the OK button is pressed, this function will be called. In the template we add another button - the cancel button will retain the alert behaviour and just hide the dialog whereas the OK button will call a function which hides the dialog and then calls the confirmPositive function.
In the controller we just need to add the function which will carry out our destructive action when the user clicks OK.
In our HTML we just need to add an attribute so that the function in our controller is passed to our directive.
A complete example can be found here.
Showing posts with label AngularJS. Show all posts
Showing posts with label AngularJS. Show all posts
Saturday, 25 June 2016
AngularJS Dialogs - Alert Dialogs
Moving from working with just jQuery to AngularJS can take some getting used to. Separating the front end from the logic of the controller throws up some interesting problems such as displaying alert and confirmation dialogs.
Here's one way to trigger a custom alert dialog from a controller.
To achieve this we create an AngularJS directive which, when activated, will cover the screen with a full-screen translucent element with a child element which will be our pop-up dialog.
We have a simple template with the full-screen background and a dialog with title, content and an OK button. When the button is clicked, visible is set to false. A link function initialises the visible variable to false and we set up some variables so that the title and content can be set and a variable mapped to the visible variable.
In our controller we can create a function to show our alert. In this example, when a button is clicked, we will set our alert message and show the alert:
The HTML including our alert directive looks like this:
A complete example can be found here.
Here's one way to trigger a custom alert dialog from a controller.
To achieve this we create an AngularJS directive which, when activated, will cover the screen with a full-screen translucent element with a child element which will be our pop-up dialog.
We have a simple template with the full-screen background and a dialog with title, content and an OK button. When the button is clicked, visible is set to false. A link function initialises the visible variable to false and we set up some variables so that the title and content can be set and a variable mapped to the visible variable.
In our controller we can create a function to show our alert. In this example, when a button is clicked, we will set our alert message and show the alert:
The HTML including our alert directive looks like this:
A complete example can be found here.
Tuesday, 2 February 2016
WCF Known Types
WCF allows you to write web service methods that take or return objects of multiple types using the KnownType attribute. This is done by defining a base class and inheriting from this class for other classes that you want to use as parameters. The base type is decorated with the KnownType attribute for each inherited type and you use the base type as the parameter on your web service method.
Here's a quick example of a base class, derived class and using the KnownType attribute on the base class. It also includes a service contract with a web service method that accepts our base class.
Here's a quick example of a base class, derived class and using the KnownType attribute on the base class. It also includes a service contract with a web service method that accepts our base class.
When implementing the web service method, we can then check the type of the class we're being passed and react accordingly.
You can call this web service from your web front-end (here I'm using AngularJS):
You can call this web service from your web front-end (here I'm using AngularJS):
$http.post(urlBase + '/Items/' + id, {'req': data});To tell WCF which type you are passing to the web service method, you need to add a property to the object in the following format (that's two underscores):
__type: DerivedClass:#MyNameSpaceAfter a lot of experimentation, I could only see the object in the web service method as the base type. I finally got it working when I discovered that the __type property has to be the first property when the object is serialized to JSON. The following solves the problem by creating a new object in JavaScript with the __type property and then adds the properties from the original object:
var d = {__type: 'DerivedClass:#MyNameSpace'};for (var i in data) {d[i] = data[i];}$http.post(urlBase + '/Items/' + id, {'req': d});
Subscribe to:
Posts (Atom)