0

我正在Visual Studio中使用涟漪运行应用程序。 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. 
     }); 
    } 
}]) 

routers.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>

回答

2

在你的控制器定义你写

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

我想这应该是作为

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

希望这有助于!

0

您的client未定义。很可能(如果您可以使用开发人员工具)尝试在页面生命周期中尽早对其进行初始化。直到页面onReady事件触发后才能初始化客户端。

我不是一个离子专家,但我相信$ionicPlatform.ready()是你需要看的东西。

相关问题