Angular: ng-show and features

Say you have a template like

<a ng-show=function()>a link</a>

My question is: when does the function start? How can I tell angular that it is time to re-run the function?

+4
source share
3 answers

Well, ng-show takes a boolean, so the best option to use it is to call your function somewhere in the logic that installs ng-show.

$scope.show = false;

$scope.showSomething = function(myCondition){
    if(myCondition){
        myFunction();
        $scope.show = true;
    }
};
<div ng-show='show'></div>
Run codeHide result
+2
source

ng-show="booleanVar"accepts a condition or logical value. You can change the value of a condition expression to hide and show values ​​at runtime.

But if you want to call some function when starting ng-show, you can use $watchin your model.

<div ng-app="myApp" ng-controller="myCtrl" ng-init="isDisplayed = true">
    <div ng-show="isDisplayed">something</div>
    <button ng-click="isDisplayed = !isDisplayed">Toggle</button>
</div>

var myApp = angular.module('myApp', [])
.controller('myCtrl', function($scope, $log) {
    $scope.$watch('isDisplayed', function(newValue, oldValue) {
        if (newValue !== oldValue) {
            $log.log('Changed!');
        }
    });
});

More here

0

ng-show , . , , , . - , , ( ).

ng-show. , ( ), .

Here is a plunker that shows the function called in each digest. (Note: in the plunker example, clicking a button actually starts two digest cycles, since the click event fires one, and switching the $ scope variable causes the other.)

0
source

Source: https://habr.com/ru/post/1673158/


All Articles