このリンクを読みました: AngularJS access scope from outside js functionとコントローラーへのアクセスを取得しようとしましたが、fiddlejs の例のように index.html をセットアップしませんでした。
$routeProvider を使用するようにアプリをセットアップし、いくつかのアプリ データを共有するための簡単なサービスをセットアップします。コントローラー間で通信し、サービスを使用して通信できるようにしたいと考えています。ページからサービスにアプリデータを設定できるようにしたいので、ルートの1つで問題が発生しています。
signin.html には、いくつかの JavaScript ウィジェットを使用する一連の JavaScript があり、完了するとコールバックがあります。スクリプトから LoginController のメソッドを呼び出そうとしていますが、ng-controller でコントローラーを指定していないため、未定義になります。私のsignin.htmlのスクリプトからLoginControllerにアクセスする方法はありますか?
myapp.js
var myApp = angular.module('myApp', ['ngCookies', 'ngResource'],
function ($routeProvider, $locationProvider, $httpProvider) {
});
myApp.service('sharedPropertiesService', function () {
var isValidUser = false;
return {
setValidUser: function (userValid) { isValidUser = userValid;},
isValidUser: function () {return isValidUser;},
};
});
myApp.controller('loginController', ['$scope', '$http', 'sharedPropertiesService',
function ($scope, $http, sharedPropertiesService) {
$scope.test = function() {alert('called from inside the html template.');};
}
]).controller('PageController', ['$scope', '$location', 'sharedPropertiesService',
function ($scope, $location, sharedPropertiesService) {
if (sharedPropertiesService.isValidUser()) {
$location.path('/myapp');
} else {
// Have them login
$location.path('/signin/');
}
}
]);
myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/', {templateUrl: '/home.html', controller: 'PageController'}).
when('/signin', {templateUrl: '/signin.html', controller: 'SusiController'}).
when('/myap', {templateUrl: '/myApp.html', controller: 'PageController'});
}]);
index.html
<html ng-app="myApp">
<head>
// load Angularjs, jquery, etc.
</head>
<body>
<div id='myapp' ng-view ng-cloak></div>
</body>
</html>
signin.html
<div id="loginwidget">
<div role="main" id="content" >
<div id="signIn" >
</div>
<div>
<div>
<script>
$.ready('loginwidget_main', function () {
loginwidget.load('signin', function () {
done : success,
fail : fail
});
});
function success() {
var scope = angular.element($('#myapp')).scope().test;
alert(scope); // this shows undefined
scope();
}
function fail() {alert("failed to login");}
</script>