Does Angular need a web server to run? So would Angular work if I give browser a url of a local file?

前端 未结 5 1271
无人共我
无人共我 2021-02-12 10:55

By this I mean I have read that Angular allows mock up data to be used so that RESTFul apis need not be wired up. I can think of a use case where a UX designer need only look a

5条回答
  •  你的背包
    2021-02-12 11:12

    So, the way I've done this is to create a temp service and just load that instead of from a url/file.

    Example:

    //tempUser.js
    angular.module("app").constant("tempUser", {
        firstname : "Joe",
        lastname : "Smith"
    });
    
    //userService.js
    angular.module("app").factory("userService", function ($q, tempUser) {
        return {
            load : load
        };
    
        function load(id) {
            //TODO: finish impl
            return $q.when(tempUser);
        }
    });
    

    This way the controller can still work as if you were loading from a web service.

    angular.module("app").controller("UserDetailCtrl", function (userService) {
        userService.load().then(function (user) {
            $scope.user = user;
        });
    });
    

提交回复
热议问题