代码之家  ›  专栏  ›  技术社区  ›  Nicoleta Wilskon

onload调用控制器中的函数以设置HTML中的值

  •  0
  • Nicoleta Wilskon  · 技术社区  · 6 年前
     $rootScope.customerName = null;
    
        var getCustomer = function () {
          Service.getCustomerById($scope.customerId).then(
            function (data) {
              $scope.dealers.customerName = data.customerName;
              $scope.dealers.customerNameLabel = data.customerName + ' ';
              $rootScope.customerName = ' '+ $scope.dealers.customerNameLabel;
            }
            }
          );
        };
    
     getCustomer();
    
    <div>{{customerName}}</div>
    

    onload我在控制器中调用一个函数来设置HTML中的值,但是它不起作用,值在函数内部被设置为“abc”,而当我在HTML中调用它时,它只是未定义的。需要帮助。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Anusha kurra    6 年前

    您必须手动调用摘要循环,即 $Apple($) ,如果值未更新。

    $rootScope.customerName = null;
        var getCustomer = function () {
          Service.getCustomerById($scope.customerId).then(
            function (data) {
              $scope.dealers.customerName = data.customerName;
              $scope.dealers.customerNameLabel = data.customerName + ' ';
              $rootScope.customerName = ' '+ $scope.dealers.customerNameLabel;
                $Scope.$apply();
            }
            }
          );
        };
    
     getCustomer();
    
    <div>{{customerName}}</div>