Creating a custom directive that encapsulates an Angular UI directive

I found a similar question but it lacks a satisfactory answer

Can anyone explain why the uib-datepicker-popup directive isn't functioning as an Angular UI date picker in the code below?

Is there a specific flag or compile option that I'm missing for this directive?

I have adapted the example from the datepicker-popup documentation in Angular-UI to work within a custom directive. Interestingly, it functions when applied with the controller but fails to work in my directive implementation.

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').directive('timeScale',()=>({
  template:  `
  <h1>Scope:</h1>
  <ul>
    <li>format: {{format}}</li>
    <li>dt: {{dt}}</li>
    <li>popup: {{popup}}</li>
    <li>dateOptions: {{dateOptions}}</li>
  </ul>
  <input type="text"
    class="form-control"
    uib-datepicker-popup="{{format}}"
    ng-model="dt"
    is-open="popup.opened"
    datepicker-options="dateOptions"
    ng-required="true"
    close-text="Close"/>
  `,

  link: $scope => {

    $scope.format = 'dd-MMMM-yyyy'
    $scope.dt = null;
    $scope.popup = {
      openend: false
    };

    $scope.dateOptions = {
      dateDisabled: false,
      formatYear: 'yy',
      maxDate: new Date(2020, 5, 22),
      minDate: new Date(),
      startingDay: 1
    }
  }
}));
<body ng-app="ui.bootstrap.demo">
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <time-scale></time-scale>
</body>

Answer №1

Indeed, it is possible to utilize a directive within another directive. I made some modifications to your code and it appears to be functioning correctly. Please have a look.

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').directive('timeScale',()=>({
  template:  `
  <h1>Scope:</h1>
  <ul>
    <li>format: {{format}}</li>
    <li>dt: {{dt}}</li>
    <li>popup: {{popup}}</li>
    <li>dateOptions: {{dateOptions}}</li>
  </ul>
  <input uib-datepicker-popup="{{format}}" type="text"
    class="form-control"
    ng-model="dt"
    is-open="popup.opened"
    datepicker-options="dateOptions"
    ng-required="true"
    close-text="Close" ng-click="open()" />
  `,

  link: $scope => {

    $scope.format = 'dd-MMMM-yyyy'
    $scope.dt = null;
    $scope.popup = {
      openend: false
    };
    $scope.open = function() {
       $scope.popup.opened = true;
    }
    $scope.dateOptions = {
      dateDisabled: false,
      formatYear: 'yy',
      maxDate: new Date(2020, 5, 22),
      minDate: new Date(),
      startingDay: 1
    }
  }
}));
<body ng-app="ui.bootstrap.demo">
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <time-scale></time-scale>
</body>

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

Unexpected error encountered in the return value from the filter function in AngularJS

My goal is to filter a specific Firebase path and check if a certain child exists within that path. I am interested in retrieving only the values that have a child called "responses". The code works fine when the child exists, but throws an error when it ...

Disable the moving of the DIV button with a left-margin of 0px

Having a bit of a challenge here. I'm attempting to craft my own slider using jQuery along with some css and javascript. I've managed to get my slider functioning, moving a Div 660px to the left and right upon button clicks. However, I'm h ...

Guide to playing a gif solely through an onclick event using JavaScript

Below you will find a code that is used to load a gif animation from an array of characters, and then display it within a board class using the image src. I am looking to make the gif animation play only when the displayed gif is clicked. Currently, the ...

Running a function following several iterations of angular.forEach

let values1 = [1, 2, 3]; angular.forEach(values1, function(value){ $compile(value)($scope); }); let values2 = ['a', 'b', 'c']; angular.forEach(values2, function(value){ $compile(value)($scope ...

Issue encountered while loading JSON data into DynamoDB's local instance

I have successfully set up DynamoDB local and everything is functioning as expected after going through their documentation. I have also tested their example code, which worked flawlessly. The Users table has been created with the name "Users". Below is ...

"Capture live video using the reverse camera with the HTML5-QR Code

Recently, I started utilizing a JavaScript library called html5-qrcode (https://github.com/mebjas/html5-qrcode) for scanning QR Codes directly from my browser. The performance of this library is exceptional - it's fast and seamless! https://i.sstatic ...

Is it possible to open a PDF file in a new tab using Angular 4?

When attempting to open a PDF file using the window.open() function, I encountered an issue where the window would open and close automatically, causing the file to be downloaded rather than displayed in a new tab. Despite not having any ad blockers inst ...

Are there any solutions for the malfunctioning v8 date parser?

The V8 Date parsing functionality is not functioning properly: > new Date('asd qw 101') Sat Jan 01 101 00:00:00 GMT+0100 (CET) I have attempted to use a delicate regular expression like the following: \d{1,2} (jan|feb|mar|may|jun|jul|a ...

javascript loop that runs on every second element only

After completing an ajax query, the following JavaScript code is executed. All of my images are named "pic". <script type="text/javascript> function done() { var e = document.getElementsByName("pic"); alert(e.length); for (var i = 0; ...

Issue with parameter functionality not working as expected

This code snippet is not functioning as expected. I am trying to extract and print the values from the URL parameter file:///C:/Users/laddi/Desktop/new%201.html?t=vindu&b=thind function GetURLParameterValue(param) { var pageURL = window. ...

Tips for extracting header ID from the HTML/DOM using react and typescript

I developed a unique app that utilizes marked.js to convert markdown files into HTML and then showcases the converted content on a webpage. In the following code snippet, I go through text nodes to extract all raw text values that are displayed and store t ...

Searching through an array for multiple values on the same key

I am currently working on dynamically generating a listview in jQuery. So far, I have been successful in creating the entire list dynamically. However, I now face the challenge of filtering, searching, or reducing my initial data: var recipes = [ { "name" ...

What is the best way to split two sets of radio buttons with the same name into distinct blocks in an HTML form?

For my project, I am working with two sets of radio buttons where the values are stored in a Database. Depending on another result in the HTML form, I need to display one set of radio buttons or the other. The issue arises when using the same name for all ...

"Duplicate content issue with ng-transclude causing element to render twice

Why is the transcluded directive displaying Name inside directive = Frank twice? I believed I had a good grasp on transclusion, but this specific scenario has left me puzzled. Check out this fiddle for more details <div ng-app="myApp" ng-controller=" ...

Determining when all promises have either been rejected or resolved using vanilla JavaScript promises

In an effort to transition to loading all resources in my web application asynchronously using basic JS promises, I have implemented a ScriptLoader function. Below is the code snippet for the ScriptLoader used to load .js files: function ScriptLoader() { ...

Allow access between Angular.js and Chrome by setting the "Access-Control-Allow

I am attempting to retrieve a JSON from my REST service using an Angular.js service that was created with Jersey/Spring. Interestingly, the service works perfectly in Firefox, but in Chrome I encounter the following error message: XMLHttpRequest cannot l ...

Toggle the visibility of tooltips based on a condition with uib-tooltip

I am looking for a way to toggle the functionality of a tooltip on a button. I attempted to disable it with the following code snippet: <button type="button" uib-tooltip="test" tooltip-is-open="false"> </button> However, the tooltip stil ...

Execute function prior to redirection of iframe

I have a specific challenge with an iframe on my webpage. I am looking to trigger a function right before the iframe reloads a new page, rather than after it has finished loading. I need this function to be triggered before the iframe redirects to the new ...

Submit Form using Ajax - Update text in HTML element upon click

As I am working on developing a message system, I encountered an issue where the message content is not opening correctly in my template when clicking the "See" button. My intention is to implement an Ajax call that will replace the content with jQuery .te ...

Pagination in Datatables

Here is the code snippet I am working with: $('#ldap-users, #audit-users').dataTable({ "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p& ...