AngularJS重新运行 - 如果在ng-repeat

时间:2016-02-03 10:36:38

标签: javascript angularjs

我试图在运行函数时运行附加了函数的ng-if条件。 ng-if仅在初始渲染时运行,但我想在进行$http调用后重新运行ng-if函数。

步骤:

  1. 列表呈现
  2. Ng-if运行函数以测试某些条件
  3. 用户点击链接
  4. 运行http.post功能
  5. success上,应在ng-repeat内的所有项目上再次运行ng-if。
  6. 附加到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;
        })
    }
    

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

因为您的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>