binding not working in nested controller

magna_nz

I'm new to Angular JS.

I have a few questions. Scope seems to be working with my first controller testController but not with my second controller controlspicy.

Why is not letting me print out $scope.greeting ? Shouldn't the binding work because I assigned a controller.

Here's a plunkr link which directs straight to the code.

http://plnkr.co/edit/NbED8vXNiZCqBjobrISa?p=preview

<!DOCTYPE html>
<html ng-app="newtest">

  <head>
    <script data-require="angular.js@*" data-semver="1.3.5" src="https://code.angularjs.org/1.3.5/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
    <script src="spicy.js"></script>
  </head>

  <body ng-controller="testController">
    <h1>Hello Plunker! {{message}}</h1>
    <input type="text" name="firstName" ng-model="thetext">
    {{double(thetext)}}
    <h1 ng-controller="controlspicy">new test</h1>
    <h2>{{greeting}}</h2>

  </body>

</html>

script.js

var app = angular.module("newtest", [])
    .controller("testController", ["$scope", function($scope) {

      $scope.message = "hola";

      $scope.double = function(value){
        if (value == null){
          return 0;
        }
        return value*2;
      };

    }]);

spicy.js

var appl = angular.module("thespicy", []) .controller("controlspicy", ["$scope", function($scope){

  $scope.greeting = "hello";

}]);
Scraph

As previously mentioned by Preston you need to wrap the <h2> inside a tag with ng-controller. There is one more issue however. controlspicy is defined in another module than the one you specify in ng-app. Change angular.module("thespicy", []) in spicy.js to angular.module("newtest").

You should almost never use more than one module in one app. You could however divide it into different sub-modules but if your new to Angular I would recommend using just one module to start with.

To clarify; you should only define a module once by typing angular.module("module_name", []). Notice the [] here. In this array you would put dependencies for the module (if you really wanted the 'thespicy' module you could have included it as a dependency with angular.module("newtest", ['thespicy']). If you later wanted to add a controller to the module you would reference the module with angular.module("module_name") (no []). For example:

// Define a module
angular.module('foo', []);

// Reference the previously defined module 'foo'
angular.module('foo')
.controller('barCtrl', function() { ... });

Here is a working fork of your example: http://plnkr.co/edit/rtUJGeD52ZoatoL3JgwY?p=preview btw.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS Controller not working in nested Controller

From Dev

Angularjs - data binding not working with controller

From Dev

File binding in WebApi controller not working

From Dev

Binding not working for innerHTML in nested component

From Dev

Binding nested class list from View to Controller

From Dev

Two-way binding between controller and directive with $watch in controller not working

From Dev

data-binding not working on nested ng-repeat

From Dev

Two-way binding not working for Nested directives - AngularJS

From Dev

Not working binding

From Dev

Binding not working?

From Dev

binding is not working

From Dev

Posting JSON values without quotes not working with model binding in ASP.NET MVC Web Api OData controller

From Dev

Binding nested ItemsControls to nested collections

From Dev

Binding nested ItemsControls to nested collections

From Dev

Binding strings to list in controller

From Dev

Angular controller not binding to view

From Dev

Binding nested elements

From Dev

Data Binding to Nested Properties?

From Dev

XAML nested template binding

From Dev

Binding for nested data

From Dev

How to binding nested class?

From Dev

Nested Element binding in Angular

From Dev

XAML nested template binding

From Dev

Binding to a nested property in template

From Dev

Can't get Reddit style working. Nested comments breaking via controller

From Dev

Knockoutjs css binding not working

From Dev

Parameter binding not working in WebAPI

From Dev

Visibility binding on grid not working

From Dev

Binding in WPF not working as expected