Javascript AngularJs-TypeError:无法设置属性';showGreeting';空的

Javascript AngularJs-TypeError:无法设置属性';showGreeting';空的,javascript,angularjs,typescript,Javascript,Angularjs,Typescript,我使用yeoman angularjs fullstack生成我的项目。 现在我试着根据我的喜好修改它。 我对angularJS很新,对TypeScript也很熟悉,所以请给我大量的反馈,而不仅仅是为什么它不起作用的答案。:) Main.html: <div ng-if=main.isLoggedIn()> <div ng-if="main.timeOutGreeting()"> <div ng-show="main.showGree

我使用yeoman angularjs fullstack生成我的项目。 现在我试着根据我的喜好修改它。 我对angularJS很新,对TypeScript也很熟悉,所以请给我大量的反馈,而不仅仅是为什么它不起作用的答案。:)

Main.html:

    <div ng-if=main.isLoggedIn()>
    <div ng-if="main.timeOutGreeting()">
        <div ng-show="main.showGreeting">
            <header class="hero-unit" id="banner">
                <div class="container">
                    <h1>Hi!</h1>
                    <p class="lead">Welcome back, mr b>{{main.currentUser().name}}</b></p>
                </div>
            </header>
        </div>
    </div>
</div>
其目的是“嗨!欢迎回来,先生用户”应该在消失之前只可见一小段时间(3秒)。所以请帮帮我!:)

如果
ng if=“…”
中存在无效的JS,则
ng if=“…”
不会引发浏览器错误。简单地看一下您的代码,我不认为$scope上定义了“main”。也许在控制器中,您应该添加
$scope.main=this。之后,在控制器的作用域内,main将是对控制器实例的引用。

如果
ng if=“…”
中存在无效JS,则
ng if
不会引发浏览器错误。简单地看一下您的代码,我不认为$scope上定义了“main”。也许在控制器中,您应该添加
$scope.main=this。之后,在控制器的作用域内,main将是对控制器实例的引用。

使用箭头函数:)

timeOutGreeting(){
此。$timeout(()=>{
this.showGreeting=false;
}, 3000);
}

使用箭头函数:)

timeOutGreeting(){
此。$timeout(()=>{
this.showGreeting=false;
}, 3000);
}

  • 您需要将$scope“服务”添加到控制器构造函数中

  • 您需要为所有服务添加一个静态$inject=['service1','service2'](与构造函数中的顺序完全相同)。这是angular了解所需服务的一种方法,因为它无法从构造函数中读取服务名称。这还可以确保脚本在缩小时工作

  • 您需要在$timeout内为函数使用lambda/fat arrow语法。例如,this.$timeout(()=>{this.$scope.main.showGreeting=true;})

  • lamda语法将创建对类“this object”的引用,而不是对该对象的本地函数的引用

    我希望这些提示能对您有所帮助:)

    我在打电话,很抱歉没有实际的实现示例

  • 您需要将$scope“服务”添加到控制器构造函数中

  • 您需要为所有服务添加一个静态$inject=['service1','service2'](与构造函数中的顺序完全相同)。这是angular了解所需服务的一种方法,因为它无法从构造函数中读取服务名称。这还可以确保脚本在缩小时工作

  • 您需要在$timeout内为函数使用lambda/fat arrow语法。例如,this.$timeout(()=>{this.$scope.main.showGreeting=true;})

  • lamda语法将创建对类“this object”的引用,而不是对该对象的本地函数的引用

    我希望这些提示能对您有所帮助:)

    我在打电话,很抱歉没有实际的实现示例

    'use strict';
    
    (function() {
    
    class MainController {
    
      showGreeting = true;
    
      constructor(Auth, $state, $timeout) {
        this.user = {};
        this.isLoggedIn = Auth.isLoggedIn;
        this.isAdmin = Auth.isAdmin;
        this.currentUser = Auth.getCurrentUser;
    
        this.$state = $state;
        this.$timeout = $timeout;
    
      }
    
      goToLogin() {
        this.$state.go('login');
      }
    
      timeOutGreeting() {
        this.$timeout(function() {
            this.showGreeting = false;
        }, 3000);
      }
    }
    
    angular.module('noteApp')
      .controller('MainController', MainController);
    
    })();