如何javascript选择remote-data.service

时间:2016-10-01 18:19:19

标签: javascript angularjs node.js

我买了一个应用程序。我是Javascript和AngularJS的新手。我试图遵循代码,但我不明白homeService.js上的函数如何知道必须使用remote-data.service.js

这是 home.controller.js

...
(function activate() {
        ...
        loadCategories();
        ...
    })();
...
// ...............................
function loadCategories() {
        debugger;
        homeService.getFeaturedCategories()
            .then(function(categories) {
                vm.categories = categories;
            });
    }

该函数调用homeService.getFeaturedCategories()。所以....

home.service.js

function getFeaturedCategories() {
        return dataService.getFeaturedCategories();
    }

对dataService.getFeaturedCategories()的函数调用 这是当 data.service.js 调用remoteDataService.js时,这个文件生成hhtp.get(我粘贴了data.service.js的完整代码):

(function() {
'use strict';

angular
    .module('restaurant.common')
    .factory('dataService', dataService);

dataService.$inject = ['ENV', '$injector'];

/* @ngInject */
function dataService(ENV, $injector) {
    switch(ENV.dataProvider) {
        case 'LOCAL':
            return $injector.get('localDataService');
        case 'REMOTE':
            return $injector.get('remoteDataService');
        case 'FIREBASE':
            return $injector.get('firebaseDataService');
    }

    throw new Error('Data provider is not valid');
}
})();

我想知道:

  • 该函数如何返回remoteDataService。
  • 另外我想知道如何使home.service.js中的函数 dataService.getFeaturedCategories()使该dataservice返回localDataService。因为我想加载一个本地JSON(不是一个http.get)

谢谢!

1 个答案:

答案 0 :(得分:0)

  1. 不明白问题。您的数据服务读取ENV.dataProvider一次,并返回适当的服务。在当前实现中,它无法为不同的调用返回不同的服务。
  2. 你应该在home.service中使用“localDataService”而不是“dataService”。只需更改名称。
  3. 在你的Home.service中你应该有以下几行:

    <?php
    
    require_once "dbreg.php";
    
    //if(isset($_GET['id']) == 'success') do this to get the id of all the users
    if(isset($_GET['msg']) == 'success'){
    
        echo "Data has been successfully updated";
    
    }
    
    $sqlquery = "SELECT username, passkey, email, firstname, lastname, gender, address, country, hobbies FROM regform WHERE id = '".$_GET['uid']."'";
    $resultquery = mysql_query($sqlquery);
    $row = mysql_fetch_assoc($resultquery);
    
    
    if(!empty($_POST)){
        $fname = $_POST['firstname'];
        $lasttname = $_POST['lasttname'];
        $usergender = $_POST['usergender'];
        $address = $_POST['address'];
        $country = $_POST['country'];
    
        $updateQuery = "UPDATE regform SET firstname='".$fname."', lastname='".$lasttname."', gender='".$usergender."', address='".$address."', country='".$country."' WHERE id='".$_GET['uid']."'";
    
    
        if(mysql_query($updateQuery)){
      //echo "Data has been successfully updated";
            header("Location: http://localhost/classwork2/adminedit.php?msg='success'");
        }
        else{
            echo "Data has not updated";
        }
    
    
    }
    
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Edit Details</title>
    </head>
    <body>
        <table border="1">
            <form name="edit" id="edit" method="post" action="adminedit.php" enctype="multipart/form-data">
                <tr>
                    <td>
                        <label>Firstname</label>
                    </td>
                    <td>
                        <input type="text" name="firstname" id="firstname" value="<?php echo $row['firstname'];?>">
                    </td>   
                </tr>
                <tr>
                    <td>
                        <label>Lastname</label>
                    </td>
                    <td>
                        <input type="text" name="lasttname" id="lastname" value="<?php echo $row['lastname'];?>">
                    </td>
                    <tr>
                        <td>
                            <label>Gender</label>
                        </td>
                        <td>
                            <input type="radio" name="usergender" value="male" <?php if($row['gender'] == 'male'){?> checked<?php }?>>Male</input>
                            <input type="radio" name="usergender" value="female" <?php if($row['gender'] == 'female'){?> checked<?php }?>>Female</input>
                            <input type="radio" name="usergender" value="other" <?php if($row['gender'] == 'other'){?> checked<?php }?>>Other</input>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Address</label>
                        </td>
                        <td>
                            <textarea name="address"><?php echo $row['address'];?></textarea> 
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Country</label>
                        </td>
                        <td>
    
                            <select name="country"> 
                                <option value="Abc" <?php if($row['country'] == 'Abc' ){?> selected<?php }?>>India</option>
                                <option value="Bcd" <?php if($row['country'] == 'Bcd' ){?> selected<?php }?>>Brazil</option>
                                <option value="Cde" <?php if($row['country'] == 'Cde' ){?> selected<?php }?>>Japan</option>
                            </select>
    
                        </td>
                    </tr>   
                    <tr>
                        <td>
                            <input type="submit" value="Edit">
                        </td></tr>          
                    </form>
                </body>
                </html>
    

    将其更改为:( $ inject和参数的顺序应相同。)

    homeService.$inject = ['dataService'];
    
    function homeService(dataService){
      ...
    }
    

    并在你的fn中使用localDataService。