Attempting to execute a bind function within a directive that is isolated in its own scope

Why is the functionality not working as expected?


index.html

<div ng-controller="mainController">
    <sun external-fct="changeValue(internalValue)"></sun>
</div>

controllers.js

controllers.controller('mainController', ['$scope', function ($scope) {

    $scope.changeValue = function (newValue) {
        $scope.value = newValue;
    }

    $scope.$watch('value', function (n, o) {
        if (n !== o)
            alert('the value has changed to ' + n);
    });

}]);

directives.js

directives.directive('sun', function () { return {

    scope: {
        fct: '&externalFct'
    },

    template: '<button>click me</button>',

    link: function (scope, element, attrs) {

        element.on('click', function (e) {
            scope.fct({
                internalValue : 'test'
            })
        });
    }
}});

Upon clicking the <button>, it seems that the directive's external scope does not properly track the changes in the value!

Answer №1

Oh no! I completely overlooked the need to execute a $digest() when clicking!

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 access forms and input fields in an AngularJS script without having to pass them from the HTML code?

Seeking a solution for input field validation, I have written code where input field states are passed from HTML to a script through a function. However, my goal is to directly retrieve the values in the script without passing them from the HTML when calli ...

Value comparison displayed in text boxes

Having two textboxes and a script to compare their values can sometimes result in unexpected behavior. For instance, when the value in Textbox2 comes from the database and it happens to be 0, it can cause issues. One possible solution is to allow Textbox1 ...

How can I execute a HTTP POST request in Node.js using MongoDB and Mongoose?

I have completed my schema setup and now I am looking for guidance on how to perform an HTTP POST request. I am currently utilizing MongoDB with the Mongoose framework. By implementing this, I aim to view the JSON data I have posted when accessing localhos ...

Encountering the error message "Uncaught TypeError: Unable to assign value to property 'color' of an undefined object"

I'm facing an issue where I am trying to change the color of the button text to "white" when the button is clicked. However, upon clicking the button, I encounter an error that says "Uncaught TypeError: Cannot set property 'color' of undefin ...

What is the best way to refresh an Angular scope variable with data obtained from the Google Maps Places API?

I am looking to implement a feature where users can search for locations using the Google Places API. When they click on a specific location, I want to update a parameter value in the scope. Here is a visual representation: The Google integration works s ...

What is the best way to refresh or clear the DateRangePicker calendar?

Is there a way to clear/reset selected dates in the DateRangePicker calendar when using a JQuery plugin? /* Calendar init */ $('.calendar__input').daterangepicker({ alwaysShowCalendars: true, linkedCalendars: false, sho ...

What is the best way to view all of the objects in an array?

My challenge involves an array consisting of ten objects, each with six properties to be displayed on a view. I want users to have the ability to update these properties by entering new data into inputs. How can I efficiently monitor the entire array to ...

Angular directives have unique scope behavior when working with the replace value

Looking for some guidance on a directive I have created with the following source code: var app = angular.module("myApp", ["mytemplate.html"]) var thai = angular.module("mytemplate.html", []).run(['$templateCache', function ($templateCa ...

The issue I am facing is that CKEditor is inserting the <pre> tag into my

I have been attempting to input Hindi language content into my database. However, when I make an API request, the data is added in Hindi as needed but a <pre> tag is inserted. I am using CkEditor like this: <CKEditor data={this.stat ...

interactive navigation menu with countdown feature

Looking to create an HTML/jQuery dropdown menu, but encountering a problem. The goal is for the div to slide down when the mouse hovers over the "games" navigation button and only disappear after 5 seconds of the mouse being out, not instantly. Here's ...

What is the best way to modify the color of a button within an AngularJS function by utilizing the same button?

Currently, I have a function assigned to the ng-click event on a button in order to filter a list. My goal is to change the color of the button once it has been clicked and the filtered list is displayed. I am looking for a way to achieve this within the ...

What purpose does sending null to XMLHttpRequest.send serve?

Have you ever wondered why send is often called like this? xhr.send(null) instead of just xhr.send() ? W3, MDN, and MSDN all mention that the argument is optional. Additionally, the ActiveX control seems to work without it: hr=pIXMLHTTPRequest.Create ...

Transforming the jQuery tooltip to be shown in a column layout

Hello, I am currently using the displayTag library to showcase some tables. My goal is to include a tooltip on each display:column element by utilizing jQuery. Below is the code snippet in question: <c:set var="titleName"><wp:i18n key="FILENAME" ...

What are some strategies for safeguarding a disabled button?

Is there a way to securely disable a button if the user does not have permission to access it? I currently disable it at the Page_Load event: Page_Load() { if(!userhasrights) { btn.Enabled=false; } } However, after rendering, ASP.N ...

Issues encountered with AES decryption when utilizing CryptoJS

Currently, I am facing an issue with encrypting and decrypting data using AES encryption. In my code, I am encrypting the data in javascript using crypto.js and trying to decrypt it in Java. Encryption and decryption work correctly when done in either jav ...

Update the form action URL when a specific option is selected from the dropdown menu upon clicking the submit

I would like my form to redirect to a specific page on my website based on the selection made in the <select> tag. For instance, if '2checkout' is selected, it should go to gateways/2checkout/2checkout.php. Similarly, if 'Payza' i ...

The ng-click function does not function properly when used within a script

I started using angular-bootstrap in my AngularJS project to incorporate dialogs. This is the code snippet I have in my HTML: <script type="text/ng-template" id="create.html"> <div class="modal-header"> <h3 class="modal-title">Welcom ...

Leveraging Angular's catchError method to handle errors and return

One of my challenges involves a model class that represents the server response: class ServerResponse { code: number; response: string; } Whenever I make api calls, I want the response to always be of type Observable<ServerResponse>, even in ...

The presence of Firefox Marionette has been identified by hCaptcha

Whenever I go on indeed.com using the firefox web driver in Marionette mode, I keep encountering an hCaptcha popup. In an attempt to avoid this, I experimented with a user script that alters the navigator.webdriver getter to return false right at the sta ...

Why is AngularJS $http response undefined?

My $http call in AngularJS is returning undefined when I try to access the data in my controller. What could be causing this issue? Despite using .then to handle promises, the data passed to the controller seems to become undefined. Can you help me figure ...