我如何通过从主HTML的值谐音? [英] How do I pass a value from main html to partials?

查看:115
本文介绍了我如何通过从主HTML的值谐音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主文件(的index.php)有一个像我想的谐音(视图)使用的用户名某些会话信息。这些都是静态的html文件,我preFER,让他们静。

The main file (index.php) has some session information like user name that I would like to use in the partials (views). These are static html files and I prefer to keep them static.

我可以对服务器的调用从局部的控制器来获取数据。但我有当主页面加载它们的谐音被加载时,不要在以后更改的信息。

I could make a call to server from the controller of the partial to get the data. But I have the information when the main page loads and they don't change later when the partials are loaded.

如何放置在主HTML此信息,然后传递给谐音的控制器,当他们加载?

How can place this information on the main html and then pass on to the controllers of the partials when they load?

PS:我用AngularJS 1.1.5(最近开始学习AngularJS)

PS: I am using AngularJS 1.1.5 (Recently started learning AngularJS)

更新:我尝试了服务与主页设定值和控制器的谐音试图读取控制器。我无法得到它的工作。

Update: I tried a service with a controller on the main page setting value and controllers for partials trying to read. I couldn't get it to work.

推荐答案

下面就是我想要做的:

在您的index.php / HTML /不管包括捕获并返回数据的角度服务的脚本标记。

In your index.php/html/whatever include a script tag with an Angular service that captures and returns your data.

<html>
    <head>

        <!-- Include Angular -->

        <script type="text/javascript">

            // By creating this service, we can capture the data from
            // from the server and store it.
            angular.module('myApp').factory('usrData', [function () {

                var data = {};

                data.usrData = <?php echo $usrService->getUserData(); ?>;

                return data;
            }]);
        </script>
    </head>
    <body>
        <!-- Continue with HTML -->

然后,你可以有其他服务,例如在service.js,您用来收集您从您的服务器捕获您的在线数据以及其他资料你通过AJAX查询,并创建管理所有的API 。

Then, you can have another service, say in service.js, that you use to collect your inline data that you captured from your server as well as other data you've queried through AJAX and create an API to manage it all. You do this by injecting the inlined service from your HTML as a dependency into this dataServices module:

angular.module('myApp').factory('dataServices', [

    '$http', 'otherData', 'usrData', // <-- These are the module dependencies

    function ($http, otherData, usrData) { // <-- Here you use them as parameters

        'use strict';

        var dataModel = {

            otherData: otherData,
            usrData: usrData
        };

        return { // Data service API

            query: function (dataType) {

                // returns data

                // API continues …

最后,在你的控制器,说controller.js,你注入这种 DataService的服务作为一个依赖检索并通过这个API保存数据:

Lastly, in your controller, say controller.js, you inject this dataServices service as a dependency to retrieve and save your data through this API:

angular.module('myApp').controller('UsrWidget', [

    '$scope', 'dataServices',

    function ctrlrPtHeader($scope, dataServices) {

        "use strict";

        $scope.usrData = dataServices.query('usrData');

        // And now you have the data.

现在,控制器的可能的直接访问内联到你的HTML usrData服务,但我preFER更多的是全球性的API,通过提供一个很好的API脚本并保存管理自己的数据我的HTML尽可能干净。秘密到整个事情是封装了所有的数据角的环境内,只是依赖注入和工厂方法通过周围的信息。

Now, the controller could directly access the usrData service that is inlined into you HTML, but I prefer more of a global API to manage my data by providing a nice API script and keeping my HTML as clean as possible. The secret to the whole thing is encapsulating all your data inside of Angular's environment and just pass the information around with dependency injection and factory methods.

这篇关于我如何通过从主HTML的值谐音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆