AngularJS ng-controller not working

前端 未结 8 1406
日久生厌
日久生厌 2020-12-09 16:30

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 c

相关标签:
8条回答
  • 2020-12-09 17:07

    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

    enter image description here

    0 讨论(0)
  • 2020-12-09 17:10

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

    The documentation says:

    AngularJS applications cannot be nested within each other.

    0 讨论(0)
  • 2020-12-09 17:12

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

    0 讨论(0)
  • 2020-12-09 17:14

    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';
    });
    
    0 讨论(0)
  • 2020-12-09 17:16

    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. Just do all steps starting from step 1.

    0 讨论(0)
  • 2020-12-09 17:17

    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.

    0 讨论(0)
提交回复
热议问题