Basically the button click will invoke ShowHide()
function from MyController
, and if $scope.IsVisible==false
, copy the value.
But here, the word this is pointing to the $scope
of formController
.
And the out put of console.log proved this, although this is what im expecting to do, but can someone explain me how did this happen? Is that because of Javascript prototype?
<div ng-app="MyApp" ng-controller="MyController">
<div ng-controller="formController">
<div ng-bind="broker.info"></div>
<div ng-show="IsVisible">Some content to be hide</div>
<button ng-click="ShowHide()">ok</button>
</div>
</div>
Script:
var app = angular.module('MyApp', []);
var old = <?=json_encode($broker)?>;
app.controller('formController',function ($scope) {
$scope.broker = angular.copy(old);
});
app.controller('MyController', function ($scope) {
$scope.IsVisible = false;
$scope.ShowHide = function () {
//If DIV is visible it will be hidden and vice versa.
$scope.IsVisible = $scope.IsVisible ? false : true;
if($scope.IsVisible==false) {
this.broker = angular.copy(old);
console.log(this);
console.log($scope);
}
}
});
Screenshot: