Javascript AngularJS ng-controller 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26602883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:07:30  来源:igfitidea点击:

AngularJS ng-controller not working

javascripthtmlangularjsangularjs-controller

提问by UiUx

I just now started learning on AngularJS from w3schools. I am trying to practice examples what ever they have mentioned in the tutorials. Every thing works fine but when i came to "AngularJS Controllers" it is not working properly as working well in w3schools Try it Yourself ?. I ve forked my code into this fiddle example. My script looks like this:

我刚刚开始从w3schools学习 AngularJS 。我正在尝试练习他们在教程中提到的示例。一切正常,但是当我来到“AngularJS 控制器”时,它不能像在w3schools 中那样正常工作亲自尝试一下?. 我已经将我的代码分叉到这个小提琴示例中。我的脚本如下所示:

function personController($scope) {
    $scope.firstName= "John";
    $scope.lastName= "Doe";
}

Try to help me out and suggest me a good tutorial(or any free pdf file).

尝试帮助我并向我推荐一个好的教程(或任何免费的 pdf 文件)。

回答by Kutyel

This is your corrected fiddle.

这是你修正的小提琴

It is a good practice for angular that the controller definition must look something like this:

对于 angular 来说,控制器定义必须看起来像这样是一个很好的做法:

angular.module("app", []).controller("personController", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});

And, without doubt, the best tutorial ever for learning the basicsof Angular is the CodeSchoolone!

而且,毫无疑问,有史以来学习Angular基础知识的最佳教程是CodeSchool之一!

Hope this helps.

希望这可以帮助。

回答by katmanco

It is a new approach. We cannot use controllers without a module no more. Have a look at this. Just add a module and append the controller then you will have no problem.

这是一种新方法。我们不能再使用没有模块的控制器。看看这个。只需添加一个模块并附加控制器,您就没有问题。

回答by khichar.anil

I just modified your js fiddle. Please check the fiddle example

我刚刚修改了你的 js 小提琴。请检查小提琴示例

function personController($scope) {
    $scope.firstName= "John";
    $scope.lastName= "Doe";
}

There was no issue with your code except JS fiddle settings from onLoadto No wrap - in head

除了从onLoadNo wrap - in head 的JS 小提琴设置之外,您的代码没有问题

enter image description here

在此处输入图片说明

回答by Beri

You need to create an application module, and then add a controller to it. In angular it is all about dependencies. Secondly you need an app-name. The base tutorial is on their main page: https://docs.angularjs.org/tutorial/step_00I started with it, and it worked fine with me. Just do all steps starting from step 1.

您需要创建一个应用程序模块,然后向其中添加一个控制器。在 angular 中,一切都与依赖有关。其次,您需要一个应用程序名称。基础教程在他们的主页上:https: //docs.angularjs.org/tutorial/step_00我从它开始,它对我来说很好用。只需从步骤 1 开始执行所有步骤。

回答by Benjamin Solum

This is a common search result for "Angular Controller Not Working" so I thought it'd be helpful to add the solution that fixed my "controller not working" problem.

这是“Angular Controller Not Working”的常见搜索结果,所以我认为添加解决我的“控制器不工作”问题的解决方案会很有帮助。

I had my base route with $routeProviderpointing at a controller file and a templateUrl partial like so:

我的基本路由$routeProvider指向一个控制器文件和一个 templateUrl 部分,如下所示:

$routeProvider.when('/', {
    templateUrl: 'partials/home.html',
    controller: 'homePage'
});

I had several other routes and all those controllers worked, but for some reason my first route wouldn't. Turns out, I had a small logic error in my partial and that prevented ANY of my controller code from running.

我还有其他几条路线,所有这些控制器都可以工作,但由于某种原因,我的第一条路线不能。事实证明,我的部分中有一个小的逻辑错误,这阻止了我的任何控制器代码运行。

Dumb little mistake, but I was caught off guard that it was a logic issue in my partial that was preventing any controller code from running. It took a lot longer than I'd like to admit to find because the issue wasn't in my "JavaScript" even though the console errors were present. The joys of tightly coupled front-end code, amiright?

愚蠢的小错误,但我措手不及,这是我的部分中的一个逻辑问题,阻止了任何控制器代码的运行。花费的时间比我想承认的要长得多,因为即使存在控制台错误,问题也不在我的“JavaScript”中。紧密耦合的前端代码的乐趣,对吧?

回答by latifalbr

You have to input function as a parameter inside module

您必须在模块内输入函数作为参数

var app = angular.module('app',[]);
app.controller('personController',function($scope){
  $scope.firstName = 'John';
  $scope.lastName = 'Smith';
});

回答by Samreen Quyyum

by adding np-app="myApp"in any tag of html like <html ng-app = "myApp">my code works fine.

通过添加np-app="myApp"任何 html 标签,就像<html ng-app = "myApp">我的代码一样工作正常。

回答by Yevgeniy Afanasyev

Same problrm may be if you will try to nest ngApp into anothe ngApp.

如果您尝试将 ngApp 嵌套到另一个 ngApp 中,可能会出现同样的问题。

The documentation says:

文档说:

AngularJS applications cannot be nested within each other.

AngularJS applications cannot be nested within each other.