A
A
Anton2015-02-26 19:57:25
JavaScript
Anton, 2015-02-26 19:57:25

How to pass parameter from page to controller in angular.js?

The question arose of how to most correctly pass a php variable from an already rendered page to the controller without littering the window, Google suggested this option:

angular.element(document).ready(function () {
    angular.module('AppName').value('name', {{$someValue}});
    angular.bootstrap(document.html, ['AppName']);
});

But for some reason, Angular requires a nameProvider, although in none of the examples did anyone create a provider...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir T., 2015-08-02
@brud

Make a directive and pass data from php to its attribute.

<your-directive param="<?php _значение_параметра_ ?>"></your-directive>

A
Anton, 2015-11-02
@brud

You can also use the ng-init attribute, but you need the variable to be passed to the function, something like this:

<div ng-controller="SomeController as vm">
    <div ng-init="vm.init('name', '<?= $value>')">
    </div>
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question