Sunday 20 August 2017

AngularJS Tutorial : $scope.$digest()

http://www.pvladov.com/p/syntax-highlighter.html

If we use Javescript to update or apply something angular will not understand .for make it work we have use $digest or $apply .because after using Vanila or plane java script angular digest cycle will not understandthe javascript code.


<html>
<head>

    <script src="~/Scripts/angular.min.js"></script>
    <script src="~/Scripts/Customjs.js"></script>
</head>
<body ng-app="myapp">
    <div id="div1" ng-controller="Function1">
        Instance of {{ControllerName}} created
       {{green}}
        <button  id="btnClick">click</button>
    </div>
    
    <div id="div2" ng-controller="Function2">
        Instance of {{ControllerName}} created

          </div>
</body>

</html>



/// <reference path="angular.min.js" />
var app = angular.module("myapp", []);

function Function1($scope) {
    $scope.ControllerName = "Function1";
    $scope.green = "green";
    document.getElementById("btnClick").addEventListener("click", function () {
        $scope.green = "red";
        //$scope.$apply();
        $scope.$digest();
},false); } function Function2($scope) { $scope.ControllerName = "Function2"; } app.controller("Function1", Function1); app.controller("Function2", Function2);

1 comment: