简体   繁体   中英

AngularJS - why controller is not working?

I have this simple code in my app.js file:

(function(){
    var app = angular.module('searchHotels', []);

    app.controller = ("PlaceController", function(){
        this.place = shop;
    });

    var shop = {
        name: 'supermarket',
        distance: 100
    };
})();

Here is the index.html file's code:

<!doctype.html>
<html ng-app="searchHotels">

<head>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
</head>

<body>

    <div ng-controller="PlaceController as placeC">
        <h3> {{placeC.place.name}} </h3>
    </div>

</body>

</html>

In the output, my {{placC.place.name}} expression is not replaced by the data it was meant to show. It just outputs " {{placeC.place.name}} " itself. What am I missing here? Thanks!

Angular controller definition is a method call, not assignment. Correct code:

app.controller("PlaceController", function(){
    this.place = shop;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM