Pass $Scope.variavel for a javascript rating at home

Asked

Viewed 104 times

0

How to pass content within config to global variables:

//meu config
app.run(function(editableOptions, $rootScope, $filter) {
        editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
        $rootScope.$on('$routeChangeSuccess', function (e, current, pre) {
            var c = current.$$route.originalPath.length;
           $rootScope.action = current.$$route.originalPath;
        });
    });

In the view I want to reset a variable within HTML, is it possible? In the example below it does not work:

<script>
var action = '{{action}}';
</script>

I tried that:

  //meu config
    app.run(function(editableOptions, $rootScope, $filter) {
            editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
            $rootScope.$on('$routeChangeSuccess', function (e, current, pre) {
                var c = current.$$route.originalPath.length;
               $rootScope.action = '<script> var action = "'+current.$$route.originalPath+'";</script>';
            });
        });

{{action}}

Didn’t work either.

1 answer

2


You can use the window variable as global, only have to pay attention when it is changed

In Javascript

var myApp = angular.module('myApp',[]);
window.test1 = 10;
window.test2;

myApp.controller('MyCtrl',['$scope',
  function MyCtrl($scope) {
    $scope.varA = test1;
    window.test2 = 'shared with window';
  }]
);

function myFunction() {
  alert(test2);
}

In Html

<div ng-app="myApp" ng-controller="MyCtrl">

 {{varA}}

<button onclick="myFunction()">Click me</button>

</div>

http://jsfiddle.net/toninho09/zjbohcve/2/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.