繁体   English   中英

AngularJS在ng-repeat中重新运行ng-if

[英]AngularJS rerun ng-if inside ng-repeat

我正在尝试运行ng-if条件,该条件在运行功能时已附加功能。 ng-if仅在初始渲染上运行,但我想在进行$http调用后重新运行ng-if函数。

脚步:

  1. 列表已呈现
  2. Ng-if运行功能以测试某些条件
  3. 用户点击链接
  4. 运行http.post函数
  5. success ,应在ng-repeat内的所有项目上再次运行ng-if。

附加到ng-if的函数比较两个数组并返回一个布尔值。 返回值指示应该在dom中支付/呈现的内容。

ng-repeat的数组来自http.get调用。 我一直在尝试再次运行http.get调用函数,但这不会触发ng-if。 页面刷新虽然。

有什么建议么?

码:

伍重复:

      <tr ng-repeat="y in courses | filter:{Category: x.Category} | filter:search | filter:location">
            <td>
                {{y.Title}}
            </td>
            <td >
                <div ng-bind-html="y.Description | sanitize"></div>
                <a style="font-weight: 700;" href="{{y.AttachmentFiles.results[0].ServerRelativeUrl}}" target="_blank">Read more</a>
            </td>
            <td>
                {{y.Date | date:"dd-MM-yyyy"}}
            </td>
            <td>
                {{y.Location}}
            </td>
            <td>
                <a href="" ng-click="enrollUser(y)" ng-if="!isEnrolled(y.Title)" style="font-weight: 700;" >
                    <i class="fa fa-arrow-circle-right"></i> 
                    Enroll
                </a>
                <span ng-if="isEnrolled(y.Title)">
                    <i class="fa fa-check-circle-o"></i> 
                    Already Enrolled
                </span>
            </td>
        </tr>

有问题的ng-if:

               <a href="" ng-click="enrollUser(y)" ng-if="!isEnrolled(y.Title)" style="font-weight: 700;" >
                    <i class="fa fa-arrow-circle-right"></i> 
                    Enroll
                </a>
                <span ng-if="isEnrolled(y.Title)">
                    <i class="fa fa-check-circle-o"></i> 
                    Already Enrolled
                </span>

附加到ng-if的函数:

  $scope.isEnrolled = function(Title) {
                for (var i = 0; i < $scope.enrollments.length; i++) {
                    if ($scope.enrollments[i].Course === Title) {
                        console.log('Value exist');
                        return true;
                    }
                }
            }   

enrollUser()函数:

$scope.enrollUser = function(item) {
$scope.userEmail = user.get_email();
var endpointUrl = "https://xxx.xxx.com/academy/_api/lists/getbytitle('EmployeeEnrollments')/items";
    var itemPayload = {
    CourseID: item.ID,
    Category: item.Category,
    Course: item.Title,
    Status: "Pending",
    Employee: $scope.userEmail,
    __metadata: {
        type: 'SP.Data.EmployeeEnrollmentsListItem'
    }
};
$http({
       method: "POST",
       url : endpointUrl,
       data: itemPayload,  
       headers: {
                   "Content-Type" : "application/json;odata=verbose",
                   "Accept": "application/json;odata=verbose",
                   "X-RequestDigest": $("#__REQUESTDIGEST").val() 
                }          
}).success(function (data, status, headers, config) { 

    //On success display notification
    Notification.primary('You have been succesfully enrolled');

    //Reload courses
    $scope.reload();
})

}

reload()函数:

$scope.reload = function(){
//Get all courses
$http({
    method: "GET",
    url: "https://xxx.xxx.com/academy/_api/lists/getbytitle('CourseCatalogue')/items?$expand=AttachmentFiles",
    headers: {
        "Accept": "application/json;odata=verbose"
    }
}).success(function(data, status, headers, config) {
    //Save results on $scope.courses array
    $scope.courses = data.d.results;
    })
}

如果更改了表达式的返回值,Angular将自动运行ng-if表达式。 在这种情况下, isEnrolled() ,如果返回值更改,Angular将自动更新DOM。

因此,您的AJAX调用( enrollUser方法)应更新$scope.enrollments$scope.courses

因为您的ng-if调用函数。 并且该函数仅被调用一次。 呈现元素的时间。

解决方案:创建一个布尔变量并将其附加到ng-if ,现在只要该变量更改值,就会重新评估ng-if 在您的ajax调用成功之后,遍历每个课程并附加一个isEnrolled变量,然后在那里进行检查。 在您的html中,设置ng-if='y.isEnrolled'

在您的重载功能中:

$scope.reload = function(){
//Get all courses
$http({
    method: "GET",
    url: "https://intra.monjasa.com/academy/_api/lists/getbytitle('CourseCatalogue')/items?$expand=AttachmentFiles",
    headers: {
        "Accept": "application/json;odata=verbose"
    }
}).success(function(data, status, headers, config) {
    //Save results on $scope.courses array
    $scope.courses = data.d.results;

    _.each($scope.courses, function(course,i){

       course.isEnrolled = //do your logic here;   

    });

    })
}

您的html将是:

    <tr ng-repeat="y in courses | filter:{Category: x.Category} | filter:search | filter:location">
        <td>
            {{y.Title}}
        </td>
        <td >
            <div ng-bind-html="y.Description | sanitize"></div>
            <a style="font-weight: 700;" href="{{y.AttachmentFiles.results[0].ServerRelativeUrl}}" target="_blank">Read more</a>
        </td>
        <td>
            {{y.Date | date:"dd-MM-yyyy"}}
        </td>
        <td>
            {{y.Location}}
        </td>
        <td>
            <a href="" ng-click="enrollUser(y)" ng-if="y.isEnrolled" style="font-weight: 700;" >
                <i class="fa fa-arrow-circle-right"></i> 
                Enroll
            </a>
            <span ng-if="y.isEnrolled">
                <i class="fa fa-check-circle-o"></i> 
                Already Enrolled
            </span>
        </td>
    </tr>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM