我正在开发一款带有Ionic的应用程序,我的控制器变量存在问题。
这是我的index.html的一部分:
<body ng-app="starter" ng-controller="ListController" ng-init="showSearch = false; showNavigationBar = false;">
<ion-header-bar align-title="center" class="bar-positive actionbar">
<div class="item" ng-click="back()" style="background-color: #0022ff; border: none; color: white" ng-hide="previousLevelName == null">
<i class="ion-chevron-left" style="margin-left: 10px"> {{previousLevelName}}</i>
</div>
<h1 class="title">FactBook</h1>
<div class="buttons">
<button class="button button-icon icon ion-ios-mic-outline" ng-click="showNavigationBar = true"></button>
<a href="templates/navigation.html">
<button class="button button-icon icon ion-ios-browsers-outline"></button>
</a>
<button class="button button-icon icon ion-ios-search" ng-click="showSearchBar = true"></button>
</div>
</ion-header-bar>
[...]
<ion-nav-view></ion-nav-view>
</body>
这是我的控制者:
var app = angular.module('starter');
app.controller("ListController", ['$scope', '$http', function($scope, $http){
$scope.content = ["Test1", "Test2"];
$scope.previousLevelName = null;
$scope.nextLevel = function(index){
$scope.previousLevelName = "Level 1";
}
$scope.back= function(index){
//Not implemented yet
}
}]);
我想在previousLevelName
的内容发生变化时更改标题栏中的“级别1”(由其他页面更改)。但没有任何反应。可能是什么错误?
更新:
html视图:
<ion-view view-title="factBook" ng-app="starter">
<ion-content class="has-header list" ng-controller="ListController">
<div class="item" ng-repeat="con in content | filter:searchText track by $index">
<div class="item" ng-click="nextLevel($index)">
<h2>
{{con}}
</h2>
</div>
</ion-content>
</ion-view>
我认为这是因为我写ng-controller="ListController"
时会创建一个新的控制器。我可以让他们单身吗?
答案 0 :(得分:1)
我认为您的选择是:
1)创建一个Angular服务并将该变量previousLevelName
传递给它。 Angular中的所有服务都是单例,因此您可以将其注入控制器并使用它。
2)将previousLevelName
转移到$rootScope
。这基本上使它成为一个全局变量,这是一种代码气味,但会使你的应用程序工作。
如果清楚的话,请告诉我。