最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - console.log for $scope in angular js - Stack Overflow

programmeradmin4浏览0评论

Why I'm not able to console output $scope values ? I'm trying to extract user assigned values from the pages and retrieve in controller. Eventually i would like to pass this to service so multiple controllers can access these data.

HTML

<!DOCTYPE html>
<html lang="en-us" ng-app="myApp">
    <head>
            <meta http-equiv="X-UA-Compatible" content="IE=Edge">
            <meta charset="UTF-8">
            <link rel="stylesheet" href="css/bootstrap.min.css" />    
            <!-- CDN lib files -->
            <link rel="stylesheet" href=".3.6/css/bootstrap.min.css" />
            <script src=".js/1.4.8/angular.min.js"></script>
            <script src=".js/1.4.8/angular-animate.js"></script>
            <script src=".1.0/ui-bootstrap-tpls.min.js"></script>    

            <!-- custom angular script -->
            <script src="js/app.js"></script>  
    </head>
     <body>
        <div class="container">
            <div ng-controller="mainController">
                <h1>Hello world!</h1>
                <label> Please enter something</label>
                <input textarea ng-model="name"></input>
                <h4> This is what you entered : {{ name }} </h4>
                <h4 style=color:red> now filtering: {{ lower() }}</h4>
            </div>
        </div>
    </body>
</html>

APP.JS

var myApp = angular.module('myApp', []);

myApp.controller('mainController', ['$scope', '$filter', function($scope, $filter) {

    $scope.name = 'Test ';  
    $scope.lower = function(){
        return $filter('lowercase')($scope.name);
    }

    $scope.name = $scope.lower();

    console.log($scope.name);
    console.log($scope.lower());

}]);

Console will output values upon initializing but not after user make changes.

Why I'm not able to console output $scope values ? I'm trying to extract user assigned values from the pages and retrieve in controller. Eventually i would like to pass this to service so multiple controllers can access these data.

HTML

<!DOCTYPE html>
<html lang="en-us" ng-app="myApp">
    <head>
            <meta http-equiv="X-UA-Compatible" content="IE=Edge">
            <meta charset="UTF-8">
            <link rel="stylesheet" href="css/bootstrap.min.css" />    
            <!-- CDN lib files -->
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.6/css/bootstrap.min.css" />
            <script src="https://cdnjs.cloudflare./ajax/libs/angular.js/1.4.8/angular.min.js"></script>
            <script src="https://cdnjs.cloudflare./ajax/libs/angular.js/1.4.8/angular-animate.js"></script>
            <script src="https://cdnjs.cloudflare./ajax/libs/angular-ui-bootstrap/1.1.0/ui-bootstrap-tpls.min.js"></script>    

            <!-- custom angular script -->
            <script src="js/app.js"></script>  
    </head>
     <body>
        <div class="container">
            <div ng-controller="mainController">
                <h1>Hello world!</h1>
                <label> Please enter something</label>
                <input textarea ng-model="name"></input>
                <h4> This is what you entered : {{ name }} </h4>
                <h4 style=color:red> now filtering: {{ lower() }}</h4>
            </div>
        </div>
    </body>
</html>

APP.JS

var myApp = angular.module('myApp', []);

myApp.controller('mainController', ['$scope', '$filter', function($scope, $filter) {

    $scope.name = 'Test ';  
    $scope.lower = function(){
        return $filter('lowercase')($scope.name);
    }

    $scope.name = $scope.lower();

    console.log($scope.name);
    console.log($scope.lower());

}]);

Console will output values upon initializing but not after user make changes.

Share Improve this question edited Nov 24, 2016 at 0:42 Carlton 8486 silver badges17 bronze badges asked Feb 16, 2016 at 23:05 Mad-DMad-D 4,66919 gold badges57 silver badges93 bronze badges 1
  • On screen model is changing due to two-way data binding where scope is being watched. But in case of console.log(), you need to set watcher manually if you want it to console to model value. – Dr. Rahul Jha Commented May 29, 2017 at 13:53
Add a ment  | 

4 Answers 4

Reset to default 8

You need to watch the scope variable:

$scope.$watch('name', function() {
    console.log($scope.name);
});

More information: https://stackoverflow./a/15113029/5787736

Just a more "functional" version. Everyone is right, you are logging the observable, not what was observed. What you want is for console.log to be called on each change to $scope, not called once (as you have it now).

$scope.$watch("name", console.log);

You're logging only from the constructor. If you want to log each time something changes, consider a watch:

$scope.$watch("name", function() {
    console.log($scope.name);        
}

Why would you expect a controller to be rerendered when a scope variable changes? You can use scope watchers or input event listeners to listen for changes.

Here's an example with a watcher:

var myApp = angular.module('myApp', []);

myApp.controller('mainController', ['$scope', '$filter', function($scope, $filter) {

    $scope.name = 'Test ';  
    $scope.lower = function(){
        return $filter('lowercase')($scope.name);
    }

    $scope.name = $scope.lower();

    console.log($scope.name);
    console.log($scope.lower());
  
    $scope.$watch('name', function() {
        console.log($scope.name);
        console.log($scope.lower());
    });

}]);
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" class="container">
  <div ng-controller="mainController">
    <h1>Hello world!</h1>
    <label>Please enter something</label>
    <input textarea ng-model="name" />
    <h4> This is what you entered : {{ name }} </h4>
    <h4 style=color:red> now filtering: {{ lower() }}</h4>
  </div>
</div>

发布评论

评论列表(0)

  1. 暂无评论