如何使用多个ng-app并添加新的模式

时间:2019-04-16 14:15:16

标签: javascript html angularjs angular-ui-bootstrap ng-app

这是我的todo.js文件

//let example = angular.module("example", ["ngStorage"]);
example.controller("ExampleController", function($scope, $localStorage) {

    $scope.save = function() {
        let testObject = [
            {
                name:"aaa",
                lastName:"bbb"
            },
            {
                name:"ccc",
                lastName:"ddd"
            }
        ]
        let myVal = $localStorage.myKey;
        $localStorage.$reset();
        if(!myVal){
            console.log("okey");
            $localStorage.myKey = testObject;
        } else {
            myVal.push({
                name:"fff",
                lastName:"ggg"
            })
            $localStorage.myKey = myVal;
        }

        $scope.datas = $localStorage.myKey;
    }

    $scope.load = function() {
        console.log($localStorage.myKey)
    }


});*/

var app = angular.module("modalFormApp", ['ui.bootstrap']);
app.controller("modalAccountFormController", function ($scope, $modal, $log) {

        $scope.showForm = function () {
            $scope.message = "Show Form Button Clicked";
            console.log($scope.message);

            var modalInstance = $modal.open({
                templateUrl: 'modal.html',
                controller: ModalInstanceCtrl,
                scope: $scope,
                resolve: {
                    userForm: function () {
                        return $scope.userForm;
                    }
                }
            });

            modalInstance.result.then(function (selectedItem) {
                $scope.selected = selectedItem;
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        };
});

var ModalInstanceCtrl = function ($scope, $modalInstance, userForm) {
    $scope.form = {}
    $scope.submitForm = function () {
        if ($scope.form.userForm.$valid) {
            console.log('user form is in scope');
            $modalInstance.close('closed');
        } else {
            console.log('userform is not in scope');
        }
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
};

这是我的index.html文件:

<html>
    <head>
        <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
        <script src="../node_modules/angular-1.6.9/angular.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/ngStorage/0.3.10/ngStorage.min.js"></script>
        <script src="./todo.js"></script>
        <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.9.0.js"></script>
    </head>
    <body>
        <!--<div  ng-app="example">
            <div ng-controller="ExampleController">
                <button ng-click="save()">Save</button>
                <button ng-click="load()">Load</button>
                <br>
                <input type='text' ng-model='searchText' placeholder="Search..." />
                <ul>
                    <li ng-repeat="data in datas | filter:searchText">
                            {{data.name}}
                    </li>
                </ul>
            </div>
        </div>-->
        <div ng-app="modalFormApp">
            <div class="container">
                <div class="col-sm-8 col-sm-offset-2">

                    <!-- PAGE HEADER -->
                    <div class="page-header">
                        <h1>AngularJS Form Validation</h1>
                    </div>

                    <div ng-controller="modalAccountFormController">
                        <div class="page-body">
                            <button class="btn btn-primary" ng-click="showForm()">Create Account</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

最后这是我的modal.html:

<div class="modal-header">
        <h3>Create A New Account!</h3>
    </div>
    <form name="form.userForm" ng-submit="submitForm()" novalidate>
        <div class="modal-body">
            <!-- NAME -->
            <div class="form-group">
                <label>Name</label>
                <input type="text" name="name" class="form-control" ng-model="name" required>
                <p ng-show="form.userForm.name.$invalid && !form.userForm.name.$pristine" class="help-block">You name is required.</p>
            </div>

            <!-- USERNAME -->
            <div class="form-group">
                <label>Username</label>
                <input type="text" name="username" class="form-control" ng-model="user.username" ng-minlength="3" ng-maxlength="8" required>
                <p ng-show="form.userForm.username.$error.minlength" class="help-block">Username is too short.</p>
                <p ng-show="form.userForm.username.$error.maxlength" class="help-block">Username is too long.</p>
            </div>

            <!-- EMAIL -->
            <div class="form-group">
                <label>Email</label>
                <input type="email" name="email" class="form-control" ng-model="email" required>
                <p ng-show="form.userForm.email.$invalid && !form.userForm.email.$pristine" class="help-block">Enter a valid email.</p>
            </div>

        </div>
        <div class="modal-footer">
            <button type="submit" class="btn btn-primary" ng-disabled="form.userForm.$invalid">OK</button>
            <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
    </form>

当我单击按钮时,我正在尝试打开模式。我在我正在使用的另一部分上添加了注释行,但是效果很好。第二部分仅用于模态,但不起作用。我什至无法打开模态。如果有基本的方法可以与我分享?我只需要打开此模式。我可以处理剩下的一切。

1 个答案:

答案 0 :(得分:0)

从文档中:

  

使用ngApp时要牢记以下几点:

     
      
  • 每个HTML文档只能自动引导一个AngularJS应用程序。在文档中找到的第一个ngApp将用于定义根元素以作为应用程序自动引导。要在HTML文档中运行多个应用程序,必须使用angular.bootstrap手动引导它们。
  •   

有关更多信息,请参见