控制器未在View中定义

时间:2014-05-15 03:35:17

标签: javascript angularjs

角度错误:错误:参数'StoreController as store'不是函数,未定义

index.html

<!DOCTYPE html>
<!-- runs the module when the document loads -->
<html ng-app='store'>
    <head>
        <link rel='stylesheet' type='text/css' href='http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css'/>
    </head>
    <body>

        <!-- expressions -->
            <!-- directive    controller name    alias -->
        <div ng-controller='StoreController as store'>
            <h1> {{store.product.name}} </h1>
            <h2>${{store.product.price}}</h2>
            <p>{{store.product.description}}</p>
        </div>
        <script type = 'text/javascript' src='https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js'></script>
        <script type ='text/javascript' src='app.js'></script>
    </body>
</html>

app.js

// wrap the function inside a closure.
(function(){
var app = angular.module('store', []);

app.controller('StoreController', function(){
    // will be executed when the store controller is called.
    this.product = gem;
});

var gem = {
    name: 'Dodecahedron',
    price: 2.95,
    description: '. . .'
};

})();

我是角色的新手,并且每当我加载我的应用时,我都会在Chrome控制台中收到上面发布的错误。我认为一切都是正确的。我不确定为什么函数StoreController没有定义,我是不是正确地将控制器传递给我的index.html?

1 个答案:

答案 0 :(得分:5)

从代码的外观来看,您正在运行Angular 1.0.1:

<script type = 'text/javascript' 
src='https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js'></script>

...而{1.1}之前没有添加Controller as propertyName语法选项。如果您想使用该语法,请加载较新版本的AngularJS。实际上,即使您不需要这种语法,也可以加载更新的版本;)

https://developers.google.com/speed/libraries/devguide#angularjs