gpt4 book ai didi

javascript - Controller 未在 View 中定义

转载 作者:行者123 更新时间:2023-11-30 08:43:55 24 4
gpt4 key购买 nike

Angular 错误:错误:参数“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: '. . .'
};

})();

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

最佳答案

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

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

...而 Controller as propertyName 语法选项直到 1.1.5 才被添加。如果您想使用该语法,请加载更新版本的 AngularJS。实际上,即使您不需要该语法,也请加载更新的版本 ;)

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

关于javascript - Controller 未在 View 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23668812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com