javascript - 添加新页面后无法设置未定义的属性 'login'?

标签 javascript cordova azure ionic-framework azure-mobile-services

我正在 Visual Studio 中使用 Ripple 运行该应用程序。添加新页面后,JS 开发者工具显示以下错误。哪一行导致了错误?

ionic.bundle.js:25642 TypeError: Cannot set property 'login' of undefined
    at new <anonymous> (http://localhost:4400/js/controllers.js:10:18)
    at invoke (http://localhost:4400/lib/ionic/js/ionic.bundle.js:17762:17)
    at Object.instantiate (http://localhost:4400/lib/ionic/js/ionic.bundle.js:17770:27)
    at http://localhost:4400/lib/ionic/js/ionic.bundle.js:22326:28
    at Object.self.appendViewElement (http://localhost:4400/lib/ionic/js/ionic.bundle.js:56883:24)
    at Object.render (http://localhost:4400/lib/ionic/js/ionic.bundle.js:54995:41)
    at Object.init (http://localhost:4400/lib/ionic/js/ionic.bundle.js:54915:20)
    at Object.self.render (http://localhost:4400/lib/ionic/js/ionic.bundle.js:56743:14)
    at Object.self.register (http://localhost:4400/lib/ionic/js/ionic.bundle.js:56701:10)
    at updateView (http://localhost:4400/lib/ionic/js/ionic.bundle.js:62357:23) <ion-nav-view name="side-menu21" class="view-container" nav-view-transition="android">

我执行了以下步骤来添加页面。

controllers.js

.controller('LoginCtrl', ['client', function (client, $scope, $state) {
    $scope.login = function() {
        client.login("facebook").then(function succes(data){
            console.log('logged in succesfully..')
            $state.go('menu.events');
        }, function(error){
            console.log('login failed.');
            //login failed.
        });
    }
}])

router.js

  .state('menu.login', {
    url: '/login',
    views: {
        'side-menu21': {
            templateUrl: 'templates/login.html',
            controller: 'LoginCtrl'
        }
    }
  })

service.js

.factory('client', [function () {
    var client = new WindowsAzure.MobileServiceClient("https://mysite.azurewebsites.net/");
    return client;
}])
;

文件login.html已添加。

<ion-view view-title="Sign in">
    <ion-content>
        <a class="item button button-block button-assertive" ng-click="login()">
            Sign In With Facebook
        </a>
    </ion-content>
</ion-view>

还添加了元 <meta http-equiv="Content-Security-Policy" content="default-src data: gap: *; style-src 'self' 'unsafe-inline'; script-src: 'self' 'unsafe-inline' 'unsafe-eval'">以及index.html中的引用:<script src="js/azure-mobile-apps-client.min.js"></script> .

最佳答案

在您的 Controller 定义中,您编写了

.controller('LoginCtrl', ['client', function (client, $scope, $state) {...})

我认为这应该是这样

.controller('LoginCtrl', ['client', '$scope', '$state', function (client, $scope, $state) {...})

希望这有帮助!

关于javascript - 添加新页面后无法设置未定义的属性 'login'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40903223/

相关文章:

node.js - 如何在 Ionic 移动应用程序中运行 Node.js 服务器?

cordova - 为什么我的cordova bin文件夹不见了?

azure - 事件订阅存储队列ARM模板

node.js - socket.io 客户端未连接到在 socket.io 中创建的 azure 服务器

javascript - 使用缩放 css 时边缘浏览器问题

javascript - 使用 JavaScript 将变量与嵌套数组内的对象进行匹配

ios - 手机间隙 : output JS logs to XCode console

azure - 在 Azure DevOps 管道中,在 powershell 任务中使用变量会导致在执行 az login 时出现 ArgumentParseError

javascript - Google Maps API v3 自定义按钮四处移动

JavaScript 检索元素的 CSS 值