为什么我的Angular2 * ngIf不能使用返回布尔值的函数?

时间:2016-05-10 16:24:23

标签: angular

如果权限正确,我会显示一个div。 * ngIf语句使用返回布尔值的方法调用服务。如果布尔值为true,则div应该显示。

这是div的代码:

import { ShowMeService } from '../services/showme.service';

let styles = require('./admin.css');

@Component({
selector: 'admin',
  directives: [ProtectedDirective],
  template: `
    <div style="margin-top: 40px" protected>
      <div class="home centered">
        <div class="logout"><a class="btn btn-primary btn-sm" 
        role= "button"(click) = "logout()" > Logout </a></div>
        <a href = "/home"><< Launchpad</a>
        <h2>User Admin</h2 >
        A permissioned div appears below if the user has permission to view:
        <hr>
        <div *ngIf="checkPermissions(5)">
          <p>
          This is an example of a page that would only be visible to roles with proper permissions.
          </p>
          <p>
            The link to this page is not visible to certain roles.
          </p>
        </div>
      </div>
    </div>
  `,
  styles: [styles],
  providers: [ShowMeService]
})


export class Admin {
  constructor(private _showMeService: ShowMeService) {}

  checkPermissions(permission: integer) {
    this._showMeService.showme(permission);
  }
}

这是在调用时返回布尔值的服务:

import {Injectable} from 'angular2/core';

@Injectable()

export class ShowMeService {
    showme(permission: integer) {
        let bool = false;
        if (permission === 5) {
            bool = true;
            return bool;
        }
    }
}

但无论它总是无法显示,即使它显然都是真的。

1 个答案:

答案 0 :(得分:3)

我猜这是缺少的return

  checkPermissions(permission: integer) {
    return this._showMeService.showme(permission);
  }