模板:
{% load staticfiles %}
<div ng-controller="CompetitionNavController as vm" style="position: relative"
ng-show="vm.is_authenticated" ng-click="vm.test();">
{{ vm.authenticated_user }} User
</div>
控制器:
(function () {
'use strict';
angular
.module('sparro.nav.controllers')
.controller('CompetitionNavController', CompetitionNavController);
CompetitionNavController.$inject = ['$cookies', '$location',
'$localStorage', '$mdDialog',
'$rootScope', '$scope', 'Auth'];
function CompetitionNavController($cookies, $location, $localStorage,
$mdDialog, $rootScope, $scope, Auth) {
var vm = this;
vm.is_authenticated = true;
vm.authenticated_user = null;
vm.test = test;
activate();
function activate() {
vm.is_authenticated = isAuthenticated();
vm.authenticated_user = $localStorage.user;
$scope.staticUrl = staticUrl;
}
function isAuthenticated() {
return Auth.isAuthenticated();
}
function test() {
console.log('It works!');
}
}
})();
如果我在模板中用 ng-if 替换 ng-show ,那么整个模板根本不会加载。如果我单击用户文本,它将调用控制器中的正确功能。我尝试使用$ scope。相同的结果
我做错了什么?控制台中也没有错误。