模型(和视图)没有通过指令内的内部函数进行更新

时间:2021-10-07 20:27:59

so I've got a directive whose controller holds some user information. I've got an inner function that fetches the user info and attempts to update the controllers scope but in my view, I can't access the inner functions value.

我有一个指令,它的控制器包含一些用户信息。我有一个内部函数,它获取用户信息并尝试更新控制器范围,但在我看来,我无法访问内部函数值。

Here is my code:

这是我的代码:

(function() {
  'use strict';

  angular
    .module('app')
    .directive('headerBar', headerBar);

  /** @ngInject */
  function headerBar() {
    var directive = {
      restrict: 'E',
      templateUrl: 'app/components/headerbar/headerbar.html',
      scope: {
        creationDate: '='
      },
      controller: HeaderbarController,
      controllerAs: 'vm',
      bindToController: true
    };

    return directive;

    /** @ngInject */
    function HeaderbarController($stamplay, $log) {
      var vm = this;
      var user = $stamplay.User().Model;
      vm.userInfo = {};
      vm.userInfo.displayName = "blah blah";

      user.currentUser()
        .then(function() {
          //User info
          vm.userInfo = user.instance;
          $log.info("User: " + vm.userInfo.displayName);
        });
    }
    }

    })();

Here are my desperate attempts to show the value. "blah blah" shows up just fine, but the actual user name while logging properly out of the function, never makes it to the model and view:

以下是我展示价值的绝望尝试。“blah”显示得很好,但实际的用户名在正确地从函数中登录时,从未出现在模型和视图中:

<input type="text" ng-value="vm.userInfo.displayName">
<div class="pull-left">
    User: {{vm.userInfo.displayName}} 
    User: {{vm.displayName}}
</div>

Any ideas why? Thanks a ton!

任何想法为什么?谢谢很多!

1 个解决方案

#1


0  

Maybe a digest cycle is not ran after setting the value.

可能在设置值之后没有运行摘要循环。

Try changing the code to this:

尝试将代码更改为以下内容:

function HeaderbarController($scope. $stamplay, $log) {

$scope.$apply(function() {
    vm.userInfo = user.instance;
})

#1


0  

Maybe a digest cycle is not ran after setting the value.

可能在设置值之后没有运行摘要循环。

Try changing the code to this:

尝试将代码更改为以下内容:

function HeaderbarController($scope. $stamplay, $log) {

$scope.$apply(function() {
    vm.userInfo = user.instance;
})