简体   繁体   English

AngularJS ng-controller无法正常工作

[英]AngularJS ng-controller not working

I just now started learning on AngularJS from w3schools . 我刚刚开始从w3schools学习AngularJS。 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 » . 一切正常,但是当我来到“AngularJS控制器”时,它在w3schools中运行良好并不能正常工作自己动手» I ve forked my code into this fiddle example . 我把我的代码分成了这个小提琴的例子 My script looks like this: 我的脚本看起来像这样:

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文件)。

This is your corrected fiddle . 这是你纠正的小提琴

It is a good practice for angular that the controller definition must look something like this: 对于角度来说,控制器定义必须如下所示是一个很好的做法:

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

And, without doubt, the best tutorial ever for learning the basics of Angular is the CodeSchool one! 毫无疑问,学习Angular 基础知识的最佳教程是CodeSchool

Hope this helps. 希望这可以帮助。

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. 只需添加一个模块并附加控制器即可,您将没有任何问题。

I just modified your js fiddle. 我只是修改了你的小提琴。 Please check the fiddle example 请查看小提琴示例

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

There was no issue with your code except JS fiddle settings from onLoad to No wrap - in head 有你的代码,除了从的onLoad没有包 JS小提琴设置没有问题-在头

在此输入图像描述

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_00 I started with it, and it worked fine with me. 基础教程在他们的主页上: https//docs.angularjs.org/tutorial/step_00我开始使用它,它对我很好。 Just do all steps starting from step 1. 只需从步骤1开始执行所有步骤。

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 $routeProvider pointing 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. 我花了很长时间才愿意承认,因为问题不在我的“JavaScript”中,即使存在控制台错误。 The joys of tightly coupled front-end code, amiright? 紧密耦合的前端代码的乐趣,amiright?

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';
});

通过在html的任何标签中添加np-app="myApp" ,例如<html ng-app = "myApp">我的代码工作正常。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM