Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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 角JS$导致链接断开?_Javascript_Angularjs - Fatal编程技术网

Javascript 角JS$导致链接断开?

Javascript 角JS$导致链接断开?,javascript,angularjs,Javascript,Angularjs,我在angular中使用$modal,在创建并取消该模式的简单循环后,主控制器会中断: TypeError: object is not a function at http://localhost:1337/bower_components/angular/angular.min.js:178:79 at f (http://localhost:1337/bower_components/angular/angular.min.js:195:177) at h.$ev

我在angular中使用$modal,在创建并取消该模式的简单循环后,主控制器会中断:

 TypeError: object is not a function
    at http://localhost:1337/bower_components/angular/angular.min.js:178:79
    at f (http://localhost:1337/bower_components/angular/angular.min.js:195:177)
    at h.$eval (http://localhost:1337/bower_components/angular/angular.min.js:113:32)
    at h.$apply (http://localhost:1337/bower_components/angular/angular.min.js:113:310)
    at HTMLAnchorElement.<anonymous> (http://localhost:1337/bower_components/angular/angular.min.js:195:229)
    at http://localhost:1337/bower_components/angular/angular.min.js:31:225
    at r (http://localhost:1337/bower_components/angular/angular.min.js:7:290)
    at HTMLAnchorElement.c (http://localhost:1337/bower_components/angular/angular.min.js:31:207)
模式模板如下所示:

(function(){

var app = angular.module('butler-user', ['ui.bootstrap']);  

app.controller('UserController', ['$http', '$log', '$state', '$modal', '$scope', 'UserStatusService', function($http, $log, $state, $modal, $scope, UserStatusService) {
    var self = this;
    self.flash = ''; 
    self.users = [];
    self.newUser  = {};
    self.editUser = {};
    $http.get('/user')
        .success(function(data, status, headers, config) {
            self.users = data;
        })
        .error(function(data, status, headers, config){
            console.log('error getting data from /user');
            $log.error("Error in get from /user: '"+config+"'");
    });


    var CreateUserModalCtlr = function ($scope, $modalInstance, userForm) {
        $scope.userForm = {};
        $scope.submitted = false;
        $scope.validateAndCreate = function () {
            console.log('Entering validateAndCreate()');
            $scope.submitted = true;
            if ($scope.userForm.$valid) {
                console.log('Creating user with:'+JSON.stringify(self.newUser));
                $http.post('/register', self.newUser)
                .success(function(data, status, headers, config) {
                    console.log('response from /register'+data+' status:'+status)
                    if (status == 200) {
                        self.newUser = {};
                        $scope.submitted = false; 
                        $http.get('/user')
                            .success(function(data, status, headers, config) {
                                self.users = data;
                            })
                            .error(function(data, status, headers, config){
                                $log.error("Error in get from /user: '"+config+"'");
                            });

                        $modalInstance.close('closed');
                    } else {
                        self.flash = 'Error creating new user';
                    }
                })
                .error(function(data, status, headers, config) { 
                    console.log('Error in /register:'+status);
                    $modalInstance.close('closed');
                });
            }
        };

        $scope.cancelModal = function () {
            console.log('Entering cancelModal()');
            $modalInstance.dismiss('cancel');
        };

    };

    self.addUser = function () {

        var modalInstance = $modal.open({
            templateUrl: '/admin/partials/CreateUser.html',
            controller: CreateUserModalCtlr,
            scope: $scope,
            resolve: {
                userForm: function () {
                    return $scope.userForm;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
<div class="modal-header">
    <h3>Create New User</h3>
</div>

<div class="modal-body">

    <form name="userForm" novalidate>
      <div class="form-group" ng-class="{ 'has-error' : userForm.firstName.$invalid && submitted }" >
        <label>First Name</label>
        <input name='firstName' class="form-control" type='text' ng-model='userCtlr.newUser.firstName' required ng-minlength=1>
        <p ng-show="userForm.firstName.$invalid && submitted" class="help-block">First name is required.</p>
      </div>
      <div class="form-group" ng-class="{ 'has-error' : userForm.lastName.$invalid && submitted }">
        <label>Last Name</label>
        <input name='lastName' class="form-control" type='text' ng-model='userCtlr.newUser.lastName'required ng-minlength=1>
        <p ng-show="userForm.lastName.$invalid && submitted" class="help-block">Last name is required.</p>
      </div>
      <div class="form-group" ng-class="{ 'has-error' : userForm.username.$invalid && !userForm.username.$pristine }">
            <label>Username</label>
            <input type="email" name="username" class="form-control" ng-model="userCtlr.newUser.username">
            <p ng-show="userForm.username.$invalid && submitted" class="help-block">Enter a valid email address.</p>
        </div>
        <div class="form-group" ng-class="{ 'has-error' : userForm.username2.$invalid && !userForm.username2.$pristine }">
            <label>Confirm Username</label>
            <input type="email" name="username2" class="form-control" ng-model="userCtlr.newUser.username2">
            <p ng-show="userForm.username2.$invalid && !userForm.username2.$pristine && submitted" class="help-block">Enter a valid email address.</p>
        </div>
        <div class="form-group" ng-class="{ 'has-error' : userForm.password.$invalid && !userForm.password.$pristine }">
            <label>Password</label>
            <input type="password" name="password" class="form-control" ng-model="userCtlr.newUser.password" required ng-minlength=8>
            <p ng-show="userForm.password.$error.minlength && submitted" class="help-block">Password is too short.</p>
        </div>
      <div class="form-group" ng-class="{ 'has-error' : userForm.password2.$invalid && !userForm.password2.$pristine }">
            <label>Confirm Password</label>
            <input type="password" name="password2" class="form-control" ng-model="userCtlr.newUser.password2" required ng-minlength=8>
            <p ng-show="userForm.password2.$error.minlength && submitted" class="help-block">Password is too short.</p>
        </div>
        <br><br>
      {{registerCtlr.flash}}

</div>
<div class="modal-footer">
<button type="button" class="btn" 
        data-ng-click="cancelModal());">Cancel</button>
<button class="btn btn-primary" 
        data-ng-click="validateAndCreate();">Create User</button>
</div>
    <div clas='row' ng-controller='UserController as userCtlr'> <!-- Use bootstrap grid layout -->
        <div class="col-md-8">
            <table class='table table-striped'>
                <thead>
                    <td>Email Address</td><td>First Name</td><td>Last Name</td>
                </thead>
                <tr ng-repeat='user in userCtlr.users'>
                    <td> <a ng-click='userCtlr.editUser(user)'>{{user.emailAddress}}</a></td>
                    <td> {{user.firstName}}</td>
                    <td> {{user.lastName}}</td>

                </tr>
                <tr>
                </tr>
            </table>

            <form name='userForm' ng-submit='userCtlr.addUser()' novalidate>
                <input type='submit' value='createNewUser'/>
            </form>

        </div>
    </div>
主HTML页面如下所示:

(function(){

var app = angular.module('butler-user', ['ui.bootstrap']);  

app.controller('UserController', ['$http', '$log', '$state', '$modal', '$scope', 'UserStatusService', function($http, $log, $state, $modal, $scope, UserStatusService) {
    var self = this;
    self.flash = ''; 
    self.users = [];
    self.newUser  = {};
    self.editUser = {};
    $http.get('/user')
        .success(function(data, status, headers, config) {
            self.users = data;
        })
        .error(function(data, status, headers, config){
            console.log('error getting data from /user');
            $log.error("Error in get from /user: '"+config+"'");
    });


    var CreateUserModalCtlr = function ($scope, $modalInstance, userForm) {
        $scope.userForm = {};
        $scope.submitted = false;
        $scope.validateAndCreate = function () {
            console.log('Entering validateAndCreate()');
            $scope.submitted = true;
            if ($scope.userForm.$valid) {
                console.log('Creating user with:'+JSON.stringify(self.newUser));
                $http.post('/register', self.newUser)
                .success(function(data, status, headers, config) {
                    console.log('response from /register'+data+' status:'+status)
                    if (status == 200) {
                        self.newUser = {};
                        $scope.submitted = false; 
                        $http.get('/user')
                            .success(function(data, status, headers, config) {
                                self.users = data;
                            })
                            .error(function(data, status, headers, config){
                                $log.error("Error in get from /user: '"+config+"'");
                            });

                        $modalInstance.close('closed');
                    } else {
                        self.flash = 'Error creating new user';
                    }
                })
                .error(function(data, status, headers, config) { 
                    console.log('Error in /register:'+status);
                    $modalInstance.close('closed');
                });
            }
        };

        $scope.cancelModal = function () {
            console.log('Entering cancelModal()');
            $modalInstance.dismiss('cancel');
        };

    };

    self.addUser = function () {

        var modalInstance = $modal.open({
            templateUrl: '/admin/partials/CreateUser.html',
            controller: CreateUserModalCtlr,
            scope: $scope,
            resolve: {
                userForm: function () {
                    return $scope.userForm;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
<div class="modal-header">
    <h3>Create New User</h3>
</div>

<div class="modal-body">

    <form name="userForm" novalidate>
      <div class="form-group" ng-class="{ 'has-error' : userForm.firstName.$invalid && submitted }" >
        <label>First Name</label>
        <input name='firstName' class="form-control" type='text' ng-model='userCtlr.newUser.firstName' required ng-minlength=1>
        <p ng-show="userForm.firstName.$invalid && submitted" class="help-block">First name is required.</p>
      </div>
      <div class="form-group" ng-class="{ 'has-error' : userForm.lastName.$invalid && submitted }">
        <label>Last Name</label>
        <input name='lastName' class="form-control" type='text' ng-model='userCtlr.newUser.lastName'required ng-minlength=1>
        <p ng-show="userForm.lastName.$invalid && submitted" class="help-block">Last name is required.</p>
      </div>
      <div class="form-group" ng-class="{ 'has-error' : userForm.username.$invalid && !userForm.username.$pristine }">
            <label>Username</label>
            <input type="email" name="username" class="form-control" ng-model="userCtlr.newUser.username">
            <p ng-show="userForm.username.$invalid && submitted" class="help-block">Enter a valid email address.</p>
        </div>
        <div class="form-group" ng-class="{ 'has-error' : userForm.username2.$invalid && !userForm.username2.$pristine }">
            <label>Confirm Username</label>
            <input type="email" name="username2" class="form-control" ng-model="userCtlr.newUser.username2">
            <p ng-show="userForm.username2.$invalid && !userForm.username2.$pristine && submitted" class="help-block">Enter a valid email address.</p>
        </div>
        <div class="form-group" ng-class="{ 'has-error' : userForm.password.$invalid && !userForm.password.$pristine }">
            <label>Password</label>
            <input type="password" name="password" class="form-control" ng-model="userCtlr.newUser.password" required ng-minlength=8>
            <p ng-show="userForm.password.$error.minlength && submitted" class="help-block">Password is too short.</p>
        </div>
      <div class="form-group" ng-class="{ 'has-error' : userForm.password2.$invalid && !userForm.password2.$pristine }">
            <label>Confirm Password</label>
            <input type="password" name="password2" class="form-control" ng-model="userCtlr.newUser.password2" required ng-minlength=8>
            <p ng-show="userForm.password2.$error.minlength && submitted" class="help-block">Password is too short.</p>
        </div>
        <br><br>
      {{registerCtlr.flash}}

</div>
<div class="modal-footer">
<button type="button" class="btn" 
        data-ng-click="cancelModal());">Cancel</button>
<button class="btn btn-primary" 
        data-ng-click="validateAndCreate();">Create User</button>
</div>
    <div clas='row' ng-controller='UserController as userCtlr'> <!-- Use bootstrap grid layout -->
        <div class="col-md-8">
            <table class='table table-striped'>
                <thead>
                    <td>Email Address</td><td>First Name</td><td>Last Name</td>
                </thead>
                <tr ng-repeat='user in userCtlr.users'>
                    <td> <a ng-click='userCtlr.editUser(user)'>{{user.emailAddress}}</a></td>
                    <td> {{user.firstName}}</td>
                    <td> {{user.lastName}}</td>

                </tr>
                <tr>
                </tr>
            </table>

            <form name='userForm' ng-submit='userCtlr.addUser()' novalidate>
                <input type='submit' value='createNewUser'/>
            </form>

        </div>
    </div>
我对$modal和Angular一般来说都是新手。我怀疑以某种方式忽略模态会破坏主控制器或其变量,但我不太了解这一切是如何运作的,因此无法探究我做错了什么

感谢您的帮助

查理