Issue with Achieving Two-Way Binding in Angular 1.5 Component when using $ctrl

I am struggling with customizing some products using a component in my index.html. Ultimately, I need to calculate the total of selected products within the main controller "planosVoz" using two-way binding on the svaTotal property in the component controller.

Unfortunately, it seems like the two-way binding is not working as expected. The red highlighted section in the image below clearly shows that the svaTotal value is not updating properly. https://i.sstatic.net/avnX0.png

Upon checking the JavaScript console, I encountered the following error:

"Error: [$compile:nonassign] http://errors.angularjs.org/1.6.1/$compile/nonassign?p0=undefined&p1=svaTotal&p2=sva
M/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:6:425
oa/</u<@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:85:257
oa/</p@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:85:334
m/c<@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:130:87
Hf/this.$get</m.prototype.$digest@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:145:81
Hf/this.$get</m.prototype.$apply@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:148:76
Wc[b]</<.compile/</<@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:282:245
r.event.dispatch@https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:3:10263
r.event.add/q.handle@https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:3:8325
"

Note: The variable names are in Brazilian Portuguese (Excuse my English in this text).

Below is a simplified version of the PlanosVoz controller:

angular.module('planosVoz').controller('planosVoz', function($scope) {
     $scope.sva = 0;
     $scope.svaTotal = function() {
            return $scope.sva;
     };  ...

The code for the sva component:

angular.module('sva').component('sva', {
    templateUrl: 'app/sva/sva.template.html',
    controller: 'sva',
    bindings: {
        titulo: '@',
        logoimg: '@',
        logoid: '@',
        base: '@',
        radioname: '@',
        svaTotal: '='  /* the two-way binding issue */
    }
});

The code for the sva controller:

angular.module('sva').controller('sva', function($scope) {
        var self = this ;

        self.basePrecos = JSON.parse(
            '{'+
              '"nuvemJornaleiro": ['+
                '{"plano": "Nenhum", "valor":0 }'+
                ',{"plano": "Semanal", "valor":4.99 }'+
                ',{"plano": "Mensal", "valor":12.9 }'+
                ']'+
              ',"kantoo": ['+
                '{"plano": "Nenhum", "valor":0 }'+
                ',{"plano": "Semanal", "valor":4.99 }'+
                ',{"plano": "Mensal", "valor":12.9 }'+
                ']'+
              ',"vivoMusica": ['+
                '{"plano": "Nenhum", "valor":0 }'+
                ',{"plano": "Mensal", "valor":12.9 }'+
                ',{"plano": "Mensal 3 em 1", "valor":14.9 }'+
                ']'+
            '}'
               );

        self.valorAnterior = 0;

         self.getBase = function(nomebase) {
                return self.basePrecos[nomebase];
         };

         self.totalizaSva = function(){
           self.svaTotal = self.svaTotal - self.valorAnterior;
           self.valorAnterior = self.svaSelecao + 0;
           self.svaTotal = self.svaTotal + self.svaSelecao;
         }
    });     

The template code for sva.template.html:

<table>
    <tr>
        <td>
            <div class="secao">
            <img ng-src="{{$ctrl.logoimg}}" id="{{$ctrl.logoid}}"></img><span class="secaoTitulo"><b>{{$ctrl.titulo}}</b></span>
            </div>
            <div style="clear:both;"></div>
        </td>
    </tr>
    <tr>
        <td colspan="2" style="text-align: center">
            <form style="margin-top: 10px;">
            <span ng-repeat="item in $ctrl.getBase($ctrl.base)">
            <input type="radio" ng-name="$ctrl.radioname" ng-click="$ctrl.totalizaSva()" ng-model="$ctrl.svaSelecao" ng-value="item.valor"> {{item.plano}}  </input>
            </span>
            </form>
            <div style="text-align:center;width:100%" ng-show="$ctrl.svaSelecao">Valor do SVA: R${{$ctrl.svaSelecao}} / SvaTotal:{{$ctrl.svaTotal}}</div>
        </td>
    </tr>
</table>

Here's a simplified snippet from the index.html:

   <!DOCTYPE html>
<html>
<head>
<!-- Other head elements -->
</head>
<body ng-app="simulador" ng-controller="planosVoz" ng-cloack> 
<div class="container">

<div class="modulo">
    <!-- Your content here -->
</div>

</body>
</html>

Answer №1

Have you attempted to utilize both the planosVoz and sva modules separately? Consider using just one module to simplify the process. Also, consider creating a plunker for easier code review in the future as it can be overwhelming to sift through extensive code manually.

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

Creating a JSON hierarchy from an adjacency list

I am currently working with adjacency data that includes ID's and Parent ID's. My goal is to convert this data into hierarchical data by creating nested JSON structures. While I have managed to make it work, I encountered an issue when dealing ...

What is causing my ReactJS web application to not recognize the cookies being sent by the backend server?

I have a web application with a frontend built in ReactJS and a backend built in HapiJS. The backend is running on http://localhost:3000 and the frontend on http://localhost:1234. My goal is to implement authentication using cookies. I am using Axios in m ...

Show a directional indicator on hover in the date selection tool

I am currently using a datepicker that looks like this: https://i.sstatic.net/XsrTO.png When I hover over it, three arrows appear to change days or show the calendar. However, I would like to remove these arrows. Here is the code snippet: link: functi ...

What is causing the error when using Interfaces and Observable together?

I created a ToDoApp and integrated Firebase into my project, but I am encountering an error. ERROR in src/app/todo-component/todo-component.component.ts(25,7): error TS2740: Type 'DocumentChangeAction<{}>[]' is missing the following proper ...

How can you use a selector to filter Cheerio objects within an `each` loop?

As I work on parsing a basic webpage using Cheerio, I began to wonder about the possibilities at hand: Consider a content structure like this: <tr class="human"> <td class="event"><a>event1</a></td> <td class="nam ...

The clearfix feature is ineffective when using AngularJS

<ul class="dropdown-menu wm_search_div" ng-show="searchDivShow"> <li ng-repeat="user in searchUserList"> <a href="javascript:void(0);" class="wm_clearfix3"> <img ng-src="{{user.faceIcon}}" class="pull-left wm_se ...

Sending text content using AJAX

Recently, I've delved into the world of AJAX and JavaScript while working on a project in JSP. My goal is to submit text entered in a textarea using a JavaScript button. The first submit button worked fine initially, but upon posting, I ended up with ...

What is the best way to allow JavaScript to access a Laravel asset?

Currently, I am working on creating a slideshow and have implemented a navbar.js file with the following code: images[0] = "{{ asset('cover/deanna-j-3GZlhROZIQg-unsplash.jpg')}}"; images[1] = "{{ asset('cover/kevin-laminto ...

Encountered an issue while trying to use Figma's spellchecker extension

Despite following the instructions in the readme carefully, I am facing difficulties running the Figma spellchecker extension. Executing npm run build goes smoothly. But when I try to run npm run spellcheck, I encounter an error message in the console: co ...

Tips for Sending an Ajax POST Request

I've been using the following code snippet to initiate a POST request to my node API in order to generate a PDF. However, upon execution, my node console displays the following errors: $('#renderPDF').click(function(){ var request = $ ...

Insert an HTML tag into JSLint

Is there a way to include an HTML tag in JSLint's list of recognized tags? I'm encountering some errors with the following message: JSLint: Unrecognized tag 'foo'. How can I make the foo tag recognized by JSLint as a valid HTML tag? ...

Rearranging div placement based on the width of the screen

I am currently working on building a responsive website and I need two divs to switch positions depending on the screen width, both on initial load and when resizing. Despite my efforts in researching and trying various options, I have not been successful ...

Is it possible to adjust the CSS code linked to the HTML tag based on the specific webpage being viewed?

I am facing an issue with the homepage of my website, which uses Scrollmagic.js for smooth scrolling. In order for the sticky footer CSS to work properly on all other pages, the HTML tag needs to have a height of 100%. However, if I add this height value t ...

Discovering XMLHttpRequest Issues within a Chrome Application

Is there a way to identify XMLHttpRequest errors specifically in Chrome App? For instance, I need to be able to recognize when net::ERR_NAME_NOT_RESOLVED occurs in order to display an error message to the user. While XMLHttpRequest.onerror is activated, ...

What causes my scene to appear black until OrbitControl is activated in ThreeJS?

What causes the scene in ThreeJS to only appear after clicking and moving the cursor? The page remains black until I interact by clicking and moving, then everything shows up. I'm stumped on what the issue could be. Any assistance would be greatly ap ...

Implement the addition of a class upon hovering using AngularJS

My goal is to include a new class when hovering over the li element using Angular in the code snippet below. <li ng-mouseenter="cola-selected=true" class="pull-left" ng-class="{'selected' : cola-selected}"> <a href="interna.html"> ...

Calculating distinct values within a single key in an object

My goal is to track the occurrences of four specific string values within the same key. The issue lies in my struggle with adding multiple counters. While the first counter successfully tracks the initial condition, subsequent conditions within the if/els ...

Install Yeoman's Angular generator without using sudo with npm

Attempting to install generator-angularjs with Yo (Yeoman) without sudo: npm install -g generator-angular An error occurred: Error: EACCES, mkdir '/usr/lib/node_modules/generator-angular' Despite attempting sudo yo, it is recommended not to u ...

Hide a div when multiple classes are filtered using jQuery

I have several divs with the class .item. When the user clicks on the Classfilter submit button, all .item elements that also have at least one class from the dateClasses array (for example ['28.09.2015', '29.09.2015']) should be hidden ...

Addressing simple JavaScript "async" AJAX requests that are sequenced and reliant on each other

I'm currently developing an application that includes a dashboard on its main page. In order to address any potential loading issues, the content of the dashboard is being calculated asynchronously using Ajax. This means that users do not have to wait ...