使用双向绑定更新角度应用

时间:2017-03-03 12:42:28

标签: angular

当用户登录我的应用程序时,我想更改文本以显示“我的帐户”页面而不是“登录”页面。

这就是我所拥有的:

<li><a (click)="openLoginForm()">
    <i class="fa fa-lock"></i> 
    {{ isLoggedIn ? "My Account" : "Login / Register" }}</a>
</li>

在我的代码中:

isLoggedIn = false;
ngOnInit() {
//run some checks
isLoggedIn = true;
}

虽然我可以在console.log中看到值的变化,但实际的HTML并没有改变。

我被告知这与双向绑定有关。

随着角度的持续变化,我不能100%确定如何解决这个问题,因为过去几个月ng2发生了巨大的变化。

虽然我可以使用JQuery轻松完成这项工作,但我的任务是只使用NG2 ...

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

在功能内部,您需要使用 object(Illuminate\Database\Eloquent\Collection)#196 (1) { ["items":protected]=> array(4) { [0]=> object(App\Modules\Role\Role)#197 (24) { ["connection":protected]=> NULL ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(5) { ["id"]=> int(1) ["name"]=> string(11) "Application" ["description"]=> string(91) "Voluptatem similique pariatur iure. Et quaerat possimus laborum non sint aspernatur fugiat." ["created_at"]=> string(19) "2017-03-03 11:56:09" ["updated_at"]=> string(19) "2017-03-03 11:56:09" } ["original":protected]=> array(5) { ["id"]=> int(1) ["name"]=> string(11) "Application" ["description"]=> string(91) "Voluptatem similique pariatur iure. Et quaerat possimus laborum non sint aspernatur fugiat." ["created_at"]=> string(19) "2017-03-03 11:56:09" ["updated_at"]=> string(19) "2017-03-03 11:56:09" } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["events":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } }

因为,在函数内部对变量的引用会发生变化。您需要使用 this

this.isLoggedIn = true;

Here is a DEMO if the same