jquery function does not work with routeprovider angularjs

4

I'm doing a web application with AngularJS and made this routeprovider to single page

angular.module("app").config(function($routeProvider){
$routeProvider.when("/Proposta", {
templateUrl:"Proposta.html",
controller :"ClienteController"
});

I have function jquery to interact with a input of a form on this page proposta.html :

<script type="text/javascript">
$(function() {
  $("#valor1").maskMoney({
    symbol: 'R$ ',
    showSymbol: true,
    thousands: '.',
    decimal: ',',
    symbolStay: true
  });
})

When I'm on page index and I'm switching to page proposta.html , this function does not work.

    
asked by anonymous 03.08.2015 / 05:12

3 answers

2

You are running the maskMoney function at the time the page ends the load (via $() ). JQuery then maps the DOM object which has the ID # value1, and applies the function.

In a second moment, Angular interprets the /Proposta route mapping, and adds the elements of Proposal.html to the DOM. The problem is that the jQuery call has already been executed.

Solution: Run the .maskMoney method inside the controller ClienteController , thus ensuring that the DOM elements of proposal.html are already loaded.

    
03.08.2015 / 06:58
1

Complementing the above answer, if you create a directive, and inject into your input field, the directive will be executed After the field is created, so the JQuery code will work, I believe this is the simplest and fastest solution to the problem.

    
17.08.2015 / 21:56
1

Within the ClientController file you can create a function to start all other related to this view. For example:

$scope.initFunctions = function(){
     $("#valor1").maskMoney({
        symbol: 'R$ ',
        showSymbol: true,
        thousands: '.',
        decimal: ',',
        symbolStay: true
     });
}

or

$scope.initFunctions = function(){
   $(document).ready(function(){
     $("#valor1").maskMoney({
        symbol: 'R$ ',
        showSymbol: true,
        thousands: '.',
        decimal: ',',
        symbolStay: true
     });
   });
}

in the file Proposition.html start this function by calling it with ng-init="initFunctions ()" that all functions will be loaded.

For some reason the RouterProvider along with other modules does not have the expected operation, one solution I found was to use the angular-ui-router that works with stateProvider.

    
05.01.2017 / 21:13