Guidelines for Document State

I am looking to develop a straightforward directive that can show the status of a document. Currently, I have achieved this functionality through the controller without using a directive. However, my goal is to create a reusable directive.

https://i.sstatic.net/2i6AM.png

https://i.sstatic.net/ouReW.png

The directive would indicate the following states:

1- New: 2- Unsaved 3- Draft 4- Posted

I have created a Plunker demo: http://plnkr.co/edit/sNoHGh4c4dgYycoDAhLQ?p=preview

The directive will be placed outside the form. Is there a way for me to access its state from the directive like this:

$scope.mainform.$setSubmitted();

Answer №1

To implement the directive, expose the function through a scope variable specified by an attribute.

angular.module("myApp").directive('once', function() {
  return {
    scope: {
        //expose API 
        onceAPI: '=once'
    },
    link: function(scope, elem, attrs) {
        scope.onceAPI = scope.onceAPI || {};
        //define function
        scope.onceAPI.setPostComplete = function() {
            console.log("post complete")
            scope.onceAPI.isPosted = true;
        }
    }
  };
});

HOW TO USE

<div once="vm.onceAPI"></div>

CONTROLLER

vm.submit = function(){
  $scope.mainform.$setSubmitted();
  var postData = {in1: vm.in1};
  $http.post(url,postData).then(function() {
      vm.onceAPI.setPostComplete();
  });
};

Check out the DEMO on PLNKR.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to update the state of an array within a .then() function in React?

` After retrieving an array of points with city and state information, I attempted to convert these points to latitude and longitude by making a fetch call. However, upon trying to update the state of the newly generated array, I found that it remained ...

Guide to passing arguments to a function within Vue's v-for loop?

Is there a way to pass the parameter {{ item.id }} to my function productos.mostrarModal() within a v-for loop in vue.js? <div v-for="item of articulos"> <a href="#" onclick="productos.mostrarModal()" > <h2>{{item.name}}< ...

Unexpected outcome: POST data not returning as anticipated

My current setup involves a form (specifically an invoice) that includes a series of input fields for various charges. <tr> <td><label>Description:</label></td> <td><input type="text name="description[]" valu ...

Ensuring data integrity and security through Firebase rules and validation for unique records

I'm currently in the process of developing a cutting-edge AngularJS application where users, after authenticating with Firebase anonymous, can input their Fullname, Company, and Email address to be added to our mailing list. The main objective is to ...

What is the best way to create random integers using JavaScript?

Is there a way to create a function called drawSomething(x, y, color, boolean) that will generate random integers for the position of x and y on the canvas, randomly select a color from red, yellow, or blue, and randomly assign true or false to the boole ...

Discovering the modified or removed shape in the leaflet drawControl

Incorporated leaflet and leafletDraw into my Angular6 application, everything is functioning properly. I can trigger the create event and add a polygon to my map, but I'm facing difficulty in determining which shape is deleted or edited: ngOnInit() { ...

Using regular expressions in JavaScript to permit a particular phone number pattern of (123) 456-7890 exclusively

I have implemented a phone number validation function for a web app. However, when this information is displayed in a windows app using a specific function that populates data to controls, it only accepts phone numbers in the format of (123) 456-7890. ...

Struggling with a character entity in Javascript? Learn how to escape it and avoid any display issues (such as showing

document.title = ("welcome &rarr; farewell"); Trying to display the arrow symbol "→" but it's not showing correctly. Any tips on how to properly escape it? ...

Unlock the power of polymer iron-ajax by seamlessly linking input element data to the iron-ajax's body attribute

Having some trouble binding data from an input element to the "body" attribute of iron-ajax. In the past, using core-ajax in Polymer 0.5, I could easily bind values like so: <core-ajax id="ajax" method="POST" contentTy ...

Does JSON have a special reserved key for identifying the time?

I've noticed an interesting issue when logging the json string of a key labeled tid - it always shows up as 0. Take a look at this example: var transaction = {tid:1, type:0, time:126312736}; var transStr = JSON.stringify(transaction); console.log(tra ...

Unable to implement the hover property for the entire class in CSS

I am struggling to highlight the entire class element on hover. Even though I have specified the button class as a selector, only the background color of the anchor tag is changing when I hover over the button. What could be causing this issue and how can ...

Tips for choosing a specific value that matches a property value within a JSON dataset

Is there a way to select a specific value in JSON based on another property value? For example, I would like to pass the configuration_code and retrieve the corresponding description. configurations: Array(2) 0: configuration_code: "SPWG" d ...

Steps to trigger a modal (bootstrap 5) in react upon clicking a cell within the Full Calendar interface

My goal is to trigger a modal window using Bootstrap 5 (not React-Bootstrap library) when clicking on a day in the FullCalendar Component in React. I attempted to access the modal by using jQuery's $, but encountered the error message "$ is not a fun ...

Obtaining an authorization token through a POST request and subsequently utilizing the POST response in a GET request

After requesting a Spotify access code using the POST method and storing the response in a variable, an attempt was made to access the token using the GET method immediately after. { angular.module('app') .controller(&apo ...

Developing an HTML table with the power of JavaScript and JSON

I am having difficulty creating an HTML table using JavaScript with JSON input. In my code, I'm using a placeholder in the HTML that will be filled by a innerHTML call in Javascript: for (var i = 0; i < json.data.length; i++) { listItem = json. ...

What is the best way to retrieve data from PHP and format it into JSON for use in my jQuery script?

I need help formatting the data returned to jQuery from a query. The specific format I want is: var externalDataRetrievedFromServer = [ { name: 'Bartek', age: 34 }, { name: 'John', age: 27 }, { name: 'Elizabeth', ...

Interactive Query Box for Searching Key-Value Pairs

I'm interested in creating a customized search box for multiple GET queries using jQuery, JS, HTML and CSS. The user would first type something to prompt the red box to appear (key) and it would autofill with a default option based on what they typed. ...

Use setTimeout and setInterval with the initial input without parentheses or double quotation marks

<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> var count=0; function increaseCount(){ document.getElementById('txt').value=count; count++; setTimeout(increaseCount(),1000); } </script> </head&g ...

Arranging JSON elements according to a separate array in Angular 2 or Node.js

Looking for a solution with optimal performance, I am seeking to achieve the rearrangement of a list using either Angular2 or NodeJS. My input consists of user fruit preferences' IDs {15, 43, 55, 67, 98}; In addition, I have a JSON object containin ...

Stacking pseudo elements in CSS positioning

Hey there! I've been using before and after in my element and it's been working well, but now I'm facing a problem. When I try to set a background color for the section, the before and after elements disappear. I understand that this issue a ...