2017-02-21 13 views
0

角度JSコントローラ機能は

function studentController($scope) { 
 
      $scope.student = { 
 
       firstName: "Mahesh", 
 
       lastName: "Parashar", 
 
       fullName: function() { 
 
        var studentObject; 
 
        studentObject = $scope.student; 
 
        return studentObject.firstName + " " + studentObject.lastName; 
 
       } 
 
      }; 
 
     }
<html> 
 
<head> 
 
    <title>Angular JS Controller</title> 
 
</head> 
 
<body> 
 
    <h2> 
 
     AngularJS Sample Application</h2> 
 
    <div ng-app="" ng-controller="studentController"> 
 
     Enter first name: 
 
     <input type="text" ng-model="student.firstName"><br> 
 
     <br> 
 
     Enter last name: 
 
     <input type="text" ng-model="student.lastName"><br> 
 
     <br> 
 
     You are entering: {{student.fullName()}} 
 
    </div> 
 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"> 
 
    </script> 
 
</body> 
 
</html>
を働いていません。 {{student.fullName()}}として表示されています。

なぜ私はフルネームを取得していないのですか? 何か間違っていますか?

+0

のように、NG-アプリにルートモジュール名を追加する必要があります'あなたのコードのどこかで。それ以外の場合、あなたのアプリは角度アプリとはみなされません。 – ValLeNain

答えて

0

あなたが宣言する部分がありません: - 主な角モジュール。 ng-appアトリビュートに入れたもの(そうしないと、アングルアプリがありません) - あなたはAngularコントローラーを宣言し、メインモジュールにアタッチします。

は自分の関数の前に

var app = angular.module('myApp', []); 
app.controller('studentController',['$scope', studentController]); 

を追加し、

<div ng-app="myApp" ng-controller="studentController"> 

そして、あなたのスクリプト内で

<div ng-app="" ng-controller="studentController"> 

を交換してください。

また、HTML文書にスクリプトをリンクする場所は表示されません(Angularライブラリを読み込むための別の<script>タグがあるはずです)。これに

0

変更オブジェクトのプロパティfullName

あなたがコードでアプリケーションモジュールを初期化し、あなたがこのモジュールにコントローラ接続する必要があり
$scope.student = { 
        firstName: "Mahesh", 
        lastName: "Parashar", 
        fullName: function() { 
         return $scope.student.firstName + " " + $scope.student.lastName; 
        } 
       }; 
0

var app = angular.module('App', []); 

app.controller('controllerName', controllerFunc) 

はまた、あなたはNG-app` `になりますモジュールを宣言するか、` angular.bootstrapを使用する必要が

ng-app="App" 
関連する問題