I am trying to make my first AngularJS application and I am having a problem.
I have an input:
<input ng-model="userNameLogin" type="text" placeholder="username" class="form-control">
Button A:
<button ng-click="setActiveUser()" type="submit" class="btn btn-success">Sign in</button>
and expression:
{{ activeUser }}
I want the text to change to everything that was input, after clicking the button. For this, I have the following controller:
app.controller('View1Ctrl', ['$scope', function($scope) {
$scope.userNameLogin = "";
$scope.activeUser = "Test";
$scope.setActiveUser = function() {
$scope.activeUser = $scope.userNameLogin;
console.log($scope.activeUser);
};
}]);
The initial value of "Test" is displayed just fine, and according to the console, the value of "activeUser" also changes correctly. But the text in the view remains the same.
I saw similar questions, where a $scope.$apply()was the answer, but if you add that after console.logI get
"Error: [$ rootScope: inprog] $ is already applied in the process."
What am I missing here?
EDIT:
, , HTML, . index.html, view1.html
index.html:
<body ng-app="myApp.view1">
<nav class="navbar navbar-inverse navbar-fixed-top" ng-controller="View1Ctrl as view">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#/view1">Kwetter</a>
</div>
<div class="navbar-collapse collapse" >
<ul class="nav navbar-nav">
<li><a href="#/view1">Home</a></li>
<li><a href="#/view2">Profile</a></li>
</ul>
<form class="navbar-form navbar-right">
<div class="form-group">
<input ng-model="userNameLogin" type="text" placeholder="username" class="form-control">
</div>
<div class="form-group">
<input type="password" placeholder="password" class="form-control">
</div>
<button ng-click="setActiveUser()" type="submit" class="btn btn-success">Sign in</button>
</form>
</div>
</div>
</nav>
<div id="pagewrapper" class="container">
<div ng-view></div>
<div>Angular seed app: v<span app-version></span></div>
</div>
view1.html
<div ng-controller="View1Ctrl as view">
<div class="row">
<div class="col-md-12 pull-left">
<image ng-src="{{ view.users[0].avatar }}"/>
<input ng-model="userNameLogin" type="text" placeholder="username" class="form-control">
<button ng-click="setActiveUser()" type="submit" class="btn btn-success">Sign in</button>
{{ activeUser }}
</div>
</div>
ng-controller <div id="pagewrapper" class="container"> div view1.html, .