Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在不同控制器之间共享数据_Javascript_Angularjs - Fatal编程技术网

Javascript 在不同控制器之间共享数据

Javascript 在不同控制器之间共享数据,javascript,angularjs,Javascript,Angularjs,我想构建一个小应用程序(用于学习Angular JS),它可以定义位置列表和事件列表。该应用程序是根据以下教程开发的: 定义新事件时,我希望为新事件关联一个位置。应使用组合框选择位置 首先我定义了两个位置,比如位置1和位置2。我想以某种方式将可用位置列表(1和2)绑定到标记为“位置选择”的组合框 到目前为止,我能够将位置列表绑定到组合框,但是只有在刷新浏览器时,组合框的项目才会更新。我想能够自动刷新组合框的内容时,可用的位置列表被更改(一个新的位置被添加或位置被删除) 这是我的HTML:

我想构建一个小应用程序(用于学习Angular JS),它可以定义位置列表和事件列表。该应用程序是根据以下教程开发的:

定义新事件时,我希望为新事件关联一个位置。应使用组合框选择位置

首先我定义了两个位置,比如位置1和位置2。我想以某种方式将可用位置列表(1和2)绑定到标记为“位置选择”的组合框

到目前为止,我能够将位置列表绑定到组合框,但是只有在刷新浏览器时,组合框的项目才会更新。我想能够自动刷新组合框的内容时,可用的位置列表被更改(一个新的位置被添加或位置被删除)

这是我的HTML:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <link rel="stylesheet"
        href="./bower_components/bootstrap-css-only/css/bootstrap.min.css" />
    </head>
    <body ng-app="myEventApp">
        <div class="container" ng-controller="EventAppController" >

            <div class="page-header">
                <h1>Edit Events</h1>
            </div>
            <div class="alert alert-info" role="alert"
                ng-hide="events && events.length > 0">There are no events yet.
            </div>

            <form class="form-horizontal" role="form"
                ng-submit="addEvent(newEventName,newEventDescription)" ng-controller="LocationAppController">

                        <div >Locations: {{locations}}</div>

                <div class="form-group" ng-repeat="event in events">
                    <div class="checkbox col-xs-9">
                        <label> <strong>{{event.name}}</strong> /
                            {{event.description}}
                        </label>
                    </div>
                    <div class="col-xs-3">
                        <button class="pull-right btn btn-danger" type="button"
                            title="Delete" ng-click="deleteEvent(event)">
                            <span class="glyphicon glyphicon-trash"></span>
                        </button>
                    </div>
                </div>
                <hr />
                <div class="input-group">
                    <input type="text" class="form-control" ng-model="newEventName"
                        placeholder="Enter the name..." /> <input type="text"
                        class="form-control" ng-model="newEventDescription"
                        placeholder="Enter the description..." /> 

                    <label for="locationSelect">Location select: </label>   
                    <select
                        name="locationSelect" id="locationSelect" ng-model="data.repeatSelect">
                        <option value="">---Please select---</option>
                        <option ng-repeat="location in locations"
                            value="{{location.id}}">{{location.name}}</option>
                    </select>

                    <div class="col-md-6"></div>


                    <span class="input-group-btn">
                        <button class="btn btn-default" type="submit"
                            ng-disabled="!newEventName||!newEventDescription" title="Add">
                            <span class="glyphicon glyphicon-plus"></span>
                        </button>
                    </span>
                </div>
            </form>
        </div>


        <!-- Location -->
        <div class="container" ng-controller="LocationAppController">
            <div class="page-header">
                <h1>Edit Locations</h1>
            </div>
            <div class="alert alert-info" role="alert"
                ng-hide="locations && locations.length > 0">There are no
                locations defined yet.</div>

            <form class="form-horizontal" role="form"
                ng-submit="addLocation(newLocationName,newLocationAddress)">
                <div class="form-group" ng-repeat="location in locations">
                    <div class="checkbox col-xs-9">
                        <label> <strong>{{location.name}}</strong> /
                            {{location.address}}
                        </label>
                    </div>
                    <div class="col-xs-3">
                        <button class="pull-right btn btn-danger" type="button"
                            title="Delete" ng-click="deleteLocation(location)">
                            <span class="glyphicon glyphicon-trash"></span>
                        </button>
                    </div>
                </div>
                <hr />
                <div class="input-group">
                    <input type="text" class="form-control" ng-model="newLocationName"
                        placeholder="Enter the name..." /> <input type="text"
                        class="form-control" ng-model="newLocationAddress"
                        placeholder="Enter the address..." />
                    <!--                <label>Location: </label> -->
                    <!--                <select -->
                    <!--                    name="repeatSelect" id="repeatSelect" ng-model="data.repeatSelect"> -->
                    <!--                    <option ng-repeat="option in data.availableOptions" -->
                    <!--                        value="{{option.id}}">{{option.name}}</option> -->
                    <!--                </select>  -->

                    <div class="col-md-6"></div>


                    <span class="input-group-btn">
                        <button class="btn btn-default" type="submit"
                            ng-disabled="!newLocationName||!newLocationAddress" title="Add">
                            <span class="glyphicon glyphicon-plus"></span>
                        </button>
                    </span>
                </div>
            </form>
        </div>






        <script type="text/javascript"
            src="./bower_components/angular/angular.min.js"></script>
        <script type="text/javascript"
            src="./bower_components/angular-resource/angular-resource.min.js"></script>
        <script type="text/javascript"
            src="./bower_components/lodash/dist/lodash.min.js"></script>
        <script type="text/javascript" src="./app/eventApp.js"></script>
        <script type="text/javascript" src="./app/eventControllers.js"></script>
        <script type="text/javascript" src="./app/eventServices.js"></script>
        <script type="text/javascript" src="./app/locationControllers.js"></script>
        <script type="text/javascript" src="./app/locationServices.js"></script>
        <script type="text/css" src="./app/custom.css"></script>
    </body>
    </html>
locationControllers.js

    (function(angular) {
        var EventAppController = function($scope, Event) {


            Event.query(function(response) {
                $scope.events = response ? response : [];
            });

            $scope.addEvent = function(name, description) {
                new Event({
                    locations:[],
                    name : name,
                    description : description,
                }).$save(function(event) {
                    $scope.events.push(event);
                });
                $scope.newEventName = "";
                $scope.newEventDescription = "";
            };

            $scope.updateEvent = function(event) {
                event.$update();
            };

            $scope.deleteEvent = function(event) {
                event.$remove(function() {
                    $scope.events.splice($scope.events.indexOf(event), 1);
                });
            };
        };

        EventAppController.$inject = [ '$scope', 'Event' ];
        angular.module("myEventApp.controllers").controller("EventAppController",
                EventAppController);
    }(angular));
        (function(angular) {
          var LocationAppController = function($scope, Location) {

              Location.query(function(response) {
                $scope.locations = response ? response : [];
              });

              $scope.addLocation = function(name, address) {
                new Location({
                name: name,  
                  address: address,
                }).$save(function(location) {
                  $scope.locations.push(location);
                });
                $scope.newLocationName = "";
                $scope.newLocationAddress = "";
              };

              $scope.updateLocation = function(location) {
                location.$update();
              };

              $scope.deleteLocation = function(location) {
                location.$remove(function() {
                  $scope.locations.splice($scope.locations.indexOf(location), 1);
                });
              };

              return {
                  getLocations: function() {
                      return $scope.locations;
                  }
              }
            };

            LocationAppController.$inject = ['$scope', 'Location'];
            angular.module("myEventApp.controllers").controller("LocationAppController", LocationAppController);

        }(angular));
eventServices.js

    (function(angular) {
        var EventFactory = function($resource) {
            return $resource('/event/:id', {
                id : '@id'
            }, {
                update : {
                    method : "PUT"
                },
                remove : {
                    method : "DELETE"
                }
            });
        };

        EventFactory.$inject = [ '$resource' ];
        angular.module("myEventApp.services").factory("Event", EventFactory);


    }(angular));
    (function(angular) {

        var LocationFactory = function($resource) {
            return $resource('/location/:id', {
                id : '@id'
            }, {
                update : {
                    method : "PUT"
                },
                remove : {
                    method : "DELETE"
                }
            });
        };

        LocationFactory.$inject = [ '$resource' ];
        angular.module("myEventApp.services").factory("Location", LocationFactory);

    }(angular));
locationServices.js

    (function(angular) {
        var EventFactory = function($resource) {
            return $resource('/event/:id', {
                id : '@id'
            }, {
                update : {
                    method : "PUT"
                },
                remove : {
                    method : "DELETE"
                }
            });
        };

        EventFactory.$inject = [ '$resource' ];
        angular.module("myEventApp.services").factory("Event", EventFactory);


    }(angular));
    (function(angular) {

        var LocationFactory = function($resource) {
            return $resource('/location/:id', {
                id : '@id'
            }, {
                update : {
                    method : "PUT"
                },
                remove : {
                    method : "DELETE"
                }
            });
        };

        LocationFactory.$inject = [ '$resource' ];
        angular.module("myEventApp.services").factory("Location", LocationFactory);

    }(angular));

因此,问题是:当添加新位置/删除位置时,如何自动更新组合框的内容?谢谢。

你能做一个JSFIDLE或plunker吗?我已经为你做了一个。在代码中,从不调用函数
addLocation()
。如何添加新位置?@ShaohaoLin
addLocation()
是从HTML