2017-05-26 28 views
0

MVCコントローラスクリプトのgetメソッドをcshtmlファイルに記述したcshtmlビューページを作成しました。ボタンをクリックするとボタンRegister New Userが置かれました。これは、$state.goを使用したHTMLページを登録するためにリダイレクトする必要があります。私はapp.jsとregister.jsという名前のモジュールを作成して、APIコントローラのポストメソッドレジスタを呼び出しました。 問題はRegister New UserボタンをクリックすることでページがRegister.htmlにリロードされないことです。 誰かが CSHTMLコード 私はangularjsを使用してWeb APIプロジェクトを作成しました

   <input id="Password" type="text" ng-model="Emp.Password" placeholder="Password" required /></br> 

       <input type="submit" value="Login" /></br> 
       <input type="reset" value="Reset" /></br> 

      </div> 

 <input type="button" ng-click="Register()" value="Register New User" /> 
    </div> 
</body> 
</html> 

<script> 
    // var myapp = angular.module("myApp", ['ui.router']); 
    (function (app) { 
     'use strict'; 

     app.controller("LoginController", ["$scope", "$http", "$location", "$window", "$state", function ($scope, $http, $location, $window, $state) { 

      $scope.Emp = {}; 
      console.log("cont mvc"); 
      $scope.Submit = function (Emp) { 
       console.log("inside", $scope.Emp); 
       $http({ 
        method: "POST", 
        url: '/Webapplication2/Test/Login', 
        data: $scope.Emp, 
       }) 
       .then(function successCallback(response) { 
        console.log("response", response.data); 
        //$window.location.href = '/Home/Display'; 
        if (response.data == "CORRECT UserName and Password") { 
         console.log("if condition") 
         alert("CORRECT UserName and Password"); 
         //$state.go("Display"); 
         //$window.location.href = '/WebApplication2/Test/Display'; 


         $window.alert("Hello " + $scope.Emp.UserName); 
        } 
        else if (response.data == "INCORRECT UserName or Password") { 
         alert("INCORRECT UserName or Password"); 
        } 
       }) 
      } 
      $scope.Register = function() { 
       console.log("reg button"); 
       $state.go('Register'); 
       //$window.location.href = '/Webapplication2/Test/Register'; 
      } 
     }]); 
    })(angular.module('myApp', ['ui.router'])); 
</script> 

モジュールapp.jsコードこの問題で私を助け

(function(){ 'use strict'; (); Register.jsはコード (機能(アプリ) {

console.log("cont register"); 
app.controller("RegisterController", function ($scope, $http, $location, $window,$state) { 
    console.log("cont2"); 
    $scope.Submit = function (Emp) { 
     console.log("inside", Emp); 
     $http({ 
      method: "POST", 
      url: '/Webapplication2/Test/Register', 
      data: $scope.Emp, 
     }) 

     .then(function (response) { 
      //$window.location.href = '/Webapplication2/Test/Display'; 
      //$msgbox.alert("Hello " + $scope.Emp.UserName); 
      $window.alert("Hello " + $scope.Emp.UserName); 
     }) 
    } 
    $scope.Login = function() { 
     console.log("login button") 
     $state.go('Login'); 
     //$window.location.href = '/Webapplication2/Test/Login'; 
    } 
}); 
})(angular.module('myApp',[])); 
+0

登録ああ起こって?コンソールエラーはありませんか? –

答えて

0

は、なぜあなたは、パラメータとしてangular.module生命維持を行う。

var app = angular.module('myApp', ['ui.router']); // app is global variable we can refer anywhere by the `window` 

    // here already refer the app module 
app.controller("RegisterController", function ($scope, $http, $location, $window,$state) { 
     //...... code 
})(angular.module('myApp', ['ui.router'])); 

は次のようにあなたのコントローラを変更するあなたの生命維持パラメータを削除します以下。

app.controller("RegisterController",["$scope", "$http", "$location", "$window","$state", function ($scope, $http, $location, $window,$state) { 
      //...... code 
    }]); 

または

(function() { 

    app.controller("RegisterController",["$scope", "$http", "$location", "$window","$state", function ($scope, $http, $location, $window,$state) { 
       //...... code 
     }]) 
}()); 
+0

コンソールでこのエラーを受け取りました... Uncaught TypeError:未定義のプロパティ 'controller'を読み取ることができません –

+0

はい......しかし変更を加えましたが、まだ関数が呼び出されていません –

+0

いつ私は "Register New User"ボタンをクリックすると、アプリケーション、jsに登録する必要があります。そのregister.jsにRegister.jsを呼び出す必要があります。Register.htmlをロードするように書いてあります。 –

関連する問題