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

时间:2015-11-20 13:24:09

标签: javascript angularjs

我想构建一个小应用程序(用于学习Angular JS),它可以定义位置列表和事件列表。该应用程序是基于此处的教程开发的:http://g00glen00b.be/prototyping-spring-boot-angularjs/

在定义新事件时,我想关联新事件的位置。应使用组合框选择位置。

首先我定义了2个位置,让我们说位置1和位置2.我想以某种方式将可用位置列表(1和2)绑定到标有“位置选择”see this image

的组合框中

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

这是我的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>

这些是使用的控制器:

eventControllers.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));

locationControllers.js

        (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));

locationServices.js

    (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));

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

0 个答案:

没有答案