Christopher Christopher - 1 year ago 110
reST (reStructuredText) Question

AngularJS change variable form view on ng-if

Ok, so I have a basic variable.

$scope.categoryHeader = "";

I'm trying to group lists by their category. How do I update the variable from the view? Or is there a better way to accomplish this?

<div ng-app="SharePointAngApp" class="row" style="color:white">
<div ng-controller="spCustomerController">
<ul ng-repeat="item in items">
<div ng-if="item.Category.Title != categoryHeader" ng-change="categoryHeader = item.Category.Title">
<a href="{{item.File.ServerRelativeUrl}}">{{item.Title}}</a>



  • item1

  • item2


  • item3

Answer Source

I would call a function on ng-if, so that i can persist the value too in my scope variable too based on my condition. you could persist your $cope.categoryHeader when it returns false.

<div ng-app="app" ng-controller='ctrl'>
<div ng-repeat=" item in num"> 
    <div ng-if="display(item)" >  
        display this

angular.module('app', [])
.controller('ctrl', function($scope) {
    $scope.num = [1,2,3];
    $scope.display = function(x) {
        if (x === 2) {  
            return true;
        } else {
            return false;

A sample fiddle:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download