Angular $scope does not work

Kieron Alsmith

I'm using AngularJS 1.3.3 and jQuery 1.11.1.

The following does not bind properly to an ng-repeat (meaning it doesn't bind the DOM to the data or update when the data is updated):

var aliasApp = angular.module('aliasApp', ['ngSanitize']);
aliasApp.controller('AliasController', function($scope) {
    $scope.aliases = new Array();
    $scope.setAliases = function(v){
        var a = new Array();
        if (Object.prototype.toString.call(v) === '[object Array]') {
            for (var i = 0; i < v.length; i++) {
                a.push(new Alias(v[i]));
            }
            $scope.aliases = a;
        }
    }
});

Here is the BROKEN code in JSFiddle http://jsfiddle.net/f6njo7sq/

However, putting "aliases" onto the "this" scope does work.

var aliasApp = angular.module('aliasApp', ['ngSanitize']);
aliasApp.controller('AliasController', function($scope) {
    var self = this;
    self.aliases = new Array();
    $scope.setAliases = function(v){
        var a = new Array();
        if (Object.prototype.toString.call(v) === '[object Array]') {
            for (var i = 0; i < v.length; i++) {
                a.push(new Alias(v[i]));
            }
            self.aliases = a;
        }
    }
});

Here is the WORKING code in JSFiddle http://jsfiddle.net/86wduo52/

Does anyone know why the second one works but the first does not?

dacopenhagen

The key here is everything to do with how you are approaching $scope.

The JS code you posted is fine. It's your HTML code thats broken.

ng-repeat="alias in root.aliases"

is wrong, instead try

ng-repeat="alias in aliases"

This is because you were looking in the "wrong" scope, the 2nd piece of JS code however changed what $scope your aliases object was in, and instead placed it in the root scope.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular $scope does not work

From Dev

Angular Js scope.$apply does not work

From Dev

$scope.formName.$setPristine() does not work ; How to reset form in angular?

From Dev

Why does it work with $scope but not with `this`?

From Dev

Why does ng-click="test()" not work but onclick="angular.element(this).scope().test()" does?

From Dev

How does scope work in Io?

From Dev

How does scope work in Ruby?

From Dev

Why does the scope work like this?

From Dev

Why does $scope.$watch work but $scope.$watchCollection does not?

From Dev

Click does not work in angular

From Dev

What is the skimlinks scope and how does it work

From Java

How does the scope of variables in julia modules work?

From Dev

How does this hoisting work with block scope?

From Dev

How does this hoisting work with block scope?

From Dev

How Does Spring Batch Step Scope Work

From Dev

What is the skimlinks scope and how does it work

From Dev

Directives scope variable does not work in Jade

From Dev

How does $.ajax success scope work?

From Dev

How does $mdDialog work with promise and $scope?

From Dev

how does jsp:useBean scope attribute work?

From Dev

'React' does not work from global scope

From Dev

Why does Angular Controller need "$scope"

From Dev

Does it affect Angular performance to have everything in scope?

From Dev

Does $scope is automatically injected to controller in Angular js?

From Dev

Why does Angular Controller need "$scope"

From Dev

Angular directive does not call parent scope function

From Dev

What does '$' really mean in '$scope' angular varialble

From Dev

Angular does not pick $scope.variable_name

From Dev

Why does printing a variable in global scope work, but modifying it does not?

Related Related

HotTag

Archive