gpt4 book ai didi

javascript - Angular JS : Controller properties not shown in html

转载 作者:行者123 更新时间:2023-11-28 03:21:54 25 4
gpt4 key购买 nike

我是 Angular JS 的新手。很抱歉这个简单的问题。

我从本教程中学到了一些东西:http://courseware.codeschool.com/shaping-up-with-angular-js/Slides/level01-05.pdf

我试过一个例子。这是行不通的。 <强> http://jsfiddle.net/89wfv/1/

HTML:

<html ng-app="main">
<body>
<div ng-controller="con">

<div>{{con.desc}}</div>
<input type='button' ng-click='getDesc();' value='Get name' />
</div>
</body>
</html>

脚本

(function() {

var app = angular.module("main", []);

app.controller("con", function() {

this.desc = "test description";

this.getDesc = function() {
alert(this.desc);
}

});

})();

问题是在 div 中显示描述并通过点击提醒描述。

提前致谢。

最佳答案

你忘了 Controller 是什么东西

<div ng-controller="con as ctrl">

<div>{{ctrl.desc}}</div>
<input type='button' ng-click='ctrl.getDesc();' value='Get name' />
</div>

改为查看指南:

https://docs.angularjs.org/guide/controller

作品:

http://jsfiddle.net/pYh89/

你没有正确加载 Angular 并添加了几个语法错误。

var app = angular.module("main", []);
app.controller("con", function () {

this.name = 'foo';

this.getName = function () {
alert("foo");
};

});

关于javascript - Angular JS : Controller properties not shown in html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24060875/

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