How can I pass a Laravel variable into my AngularJS view?

后端 未结 2 1477
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-19 00:06

I am building a small photo application to learn AngularJS 1.3. I come from a PHP background so conceptually this is rather different for me :)

I\'d like to pass a v

相关标签:
2条回答
  • 2021-01-19 00:44

    I wonder whether this would be the best practice or not. However If the value doesn't need to be secure, one of the most simple way to pass constant values from server is something like this.

    In html.

    <script type="text/javascript">
        var _config = {
            key1: server_value1,
            key2: server_value2
        };
    </script>
    

    In angular module run method.

    angular.module('yourapp', [])
        .run(function($rootScope) {
            $rootScope.config = _config;
        });
    

    In your controller.

    console.log($scope.config);
    
    0 讨论(0)
  • 2021-01-19 00:49

    Did you managed to have Angular and Blade syntax in order? In my case I changed what Laravel uses for echoing variables and evaluating expressions this way setting at routes.php:

    Blade::setContentTags('[[', ']]');        // for variables and all things Blade
    Blade::setEscapedContentTags(',.', '.,');   // for escaped data
    

    I'm not sure about how a good practice is but time to time I set some ng-init directives this way:

    Blade template

    <div ng-controller="mainController" ng-init="constants={upload_url:'[[$upload_url]]'}">
      ...
    </div>
    

    View

    The way I obtain a rendered view this way:

    <div ng-controller="mainController" ng-init="constants={upload_url:'some_url_string'}">
     ...
    </div>
    

    Then you could reach it from a controller and use it for ng-Route this way:

    $scope.constants.upload_url
    
    0 讨论(0)
提交回复
热议问题