AngularJS - 为什么控制器不工作?

时间:2015-07-19 10:34:48

标签: javascript angularjs

我在app.js文件中有这个简单的代码:

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

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

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

这是index.html文件的代码:

<!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>

在输出中,我的{{placC.place.name}}表达式不会被它要显示的数据所取代。它只输出“ {{placeC.place.name}} ”本身。我在这里错过了什么?谢谢!

1 个答案:

答案 0 :(得分:5)

角度控制器定义是方法调用,而不是赋值。正确的代码:

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