Search code examples
angularjsangularjs-directiveangular-controller

AngularJs : The code i have written doesn't work and Code in comment works. where am I going wrong. Please Explain


I'm learning AngularJS from W3SCHOOLS, and I am facing this problem over and over again! In the following code uncommentated lines of controller code are written by me and multiple comment lines copied from W3SCHOOLS (Angular Controllers) . When I run this program, uncommented code doesn't work, and if i use commented code instead , program works fine. I am not getting this because I am not seeing any difference between these two blocks!

<html>
<script src="angular.min.js"></script>
<script>
    var app = angular.module("myApp", []);
    //controller code 
    app.controller = ('person', function($scope){
    $scope.fname = "Kiran";
    $scope.lname = "Chaudhari";
    $scope.fullname = function(){
    return $scope.fname + " " + $scope.lname;
    };
    });
    /*app.controller('person', function($scope) {
    $scope.fname = "Kiran";
    $scope.lname = "Chaudhari";
    $scope.fullname = function() {
        return $scope.fname + " " + $scope.lname;
    };
});*/
</script>
<body>
<div ng-app="myApp" ng-controller="person">
    First Name : <input type="text" ng-model="fname">
    Last Name : <input type="text" ng-model="lname">
    <br>
    Full Name : {{fullname()}}
</div>
</body>
</html>

Solution

  • app.controller = (); "=" there should be no equal sign in this syntax of AngularJS