【问题标题】:AngularJS variable value not updating in view?AngularJS变量值没有在视图中更新?
【发布时间】:2015-07-20 18:57:36
【问题描述】:

我有一个在控制器中调用函数的按钮。单击按钮时,它正在更新变量buttonDisplay 的值,但视图中的值不会更新。

<button type="button" class="btn btn-default btn-lg navButton" aria-label="Center Align" ng-controller = "buttonViewCtrl" ng-click="nav()">
        <span class="glyphicon glyphicon-align-justify" aria-hidden="true"></span>
</button>

<div ng-controller = "buttonViewCtrl"> My name is {{buttonDisplay}} </div>

控制器

.controller('buttonViewCtrl',  [ '$scope', function($scope) {
    $scope.buttonDisplay = 0;

    $scope.nav = function() {
        $scope.buttonDisplay++;
    };

对于上下文,我在下面为我的整个应用程序提供代码。

index.html:

<!DOCTYPE html>
<html lang="en" ng-app="myApp" class="no-js"> 
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>My AngularJS App</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/normalize.css">
    <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/main.css">

    <link rel="stylesheet" href="app.css">

    <script src="bower_components/html5-boilerplate/dist/js/vendor/modernizr-2.8.3.min.js"></script>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
    <link rel="shortcut icon" href="images/favicon.ico" />
    <link rel="stylesheet" href="css/anchour/anchour.css"/>
  </head>

  <body>
    <div ng-view></div>
    <div>Angular seed app: v<span app-version></span></div>

    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="app.js"></script>
    <script src="view1/view1.js"></script>
    <script src="view2/view2.js"></script>
    <script src="buttonView/buttonView.js"></script>
    <script src="components/version/version.js"></script>
    <script src="components/version/version-directive.js"></script>
    <script src="components/version/interpolate-filter.js"></script>
  </body>
</html>

app.js:

'use strict';

 angular.module('myApp', [
  'ngRoute',
  'myApp.view1',
  'myApp.view2',
  'myApp.version'
]).
config(['$routeProvider', function($routeProvider) {
  $routeProvider.otherwise({redirectTo: '/view1'});
}]);

view1.html:

<div class="squirrel"> 
      <div class="logo"></div>
      <cite>Every brand has a story.</cite>
      <h2> <a>Watch the video </a></h2>
      <aside> &#x2192; </aside>

      <button type="button" class="btn btn-default btn-lg navButton" aria-label="Center Align" ng-controller = "buttonViewCtrl" ng-click="go('/buttonView')">
        <span class="glyphicon glyphicon-align-justify" aria-hidden="true"></span>
      </button>

      <div id = "arrow">
        <img src = "images/pointer.png" alt = "arrow">
      </div>
    </div>

    <div class = "panda">
      <button type="button" class="btn btn-default btn-lg work"> SEE OUR WORK </button>
    </div>

    <div class = "experience">
      <h1> Our team builds great brands and experiences. </h1>
      <button type="button" class="btn btn-default btn-lg team"> MEET THE TEAM </button>
    </div>

    <section class = "about">
      <h5> WHAT ARE WE? </h5>
      <h2> Anchour is a branding agency based in Maine. </h3>

      <p> We weave together creative solutions to build personal brands and experiences. We work closely with your brand to understand your needs and create solutions that produce real results. By integrating the power of identity, digital, and sensory design, we bring new life to brands everywhere.
      </p>
    </section>

    <div class = "goodWork">
      <div class = "spotlight"> 
        <h3> <a href = "#"> Spotlight: Amanda Brill of Las Vegas </a> </h3>
        <p> Amanda Brill is a Designer at Anchour. Fueled by the purpose of helping others, she works to bring the identity of a brand to life through a creative and intensive design process. to help brands effectively communicate who they she works to bring the identity of a brand to life through a creative... </p>
        <footer> <a href = "#"> Read More </a> </footer>
      </div>

      <div class = "spotlight"> 
        <h3> <a href = "#"> Spotlight: Amanda Brill of California </a> </h3>
        <p> Amanda Brill is a Designer at Anchour. Fueled by the purpose of helping others, she works to bring the identity of a brand to life through a creative and intensive design process. Her goal is to use design as a way to help brands effectively communicate who they sponsor and supporter Fueled by the purpose of.. </p>
        <footer> <a href = "#"> Read More </a> </footer>
      </div>

      <div class = "spotlight"> 
        <h3> <a href = "#"> Varney Agency: Protecting What You </a> </h3>
        <p> Anchour produced Varney Agencys latest spot featuring Matt Albrecht, owner of River Drive. Working with companies from all around the world, River Drive buys, sells, reconditions and recycles reconditions and ecycles owner of used. River Drive buys, sells Varney Agencys latest spot featuring Matt Albrecht.</p>
        <footer> <a href = "#"> Read More </a> </footer>
      </div>

      <div class = "spotlight"> 
        <h3> <a href = "#">Announcing support of Not Here </a> </h3>
        <p> This week is Human Trafficking Awareness Week, so it’s great timing to share how proud we are to be a sponsor and supporter of Not Here latest spot featuring Matt Albrecht, reconditions and recycles owner of River Drive. Working with companies from all around the,a River Drive buys, sells.... </p>
        <footer> <a href = "#"> Read More </a> </footer>
      </div>
    </div>

    <div class = "start">
      <h2 id = "startWork"> Want to work with us? </h2>
      <button type="button" class="btn btn-default btn-lg"> Get Started &#x2192; </button>
    </div>

    <div id = "end">
      <footer>
        <a href = "#"> Home </a>
        <a href = "#"> About us </a>
        <a href = "#"> Our Team </a>
        <a href = "#"> Blog </a>
        <a href = "#"> Services </a>
        <a href = "#"> Portfolio </a>
        <a href = "#"> Contact </a>
        <a href = "#"> Sitemap </a>
    </footer>
    </div>

view1.js:

'use strict';

angular.module('myApp.view1', ['ngRoute'])
    .config(['$routeProvider', function($routeProvider) {
        $routeProvider.
            when('/view1', {
                templateUrl: 'view1/view1.html',
                controller: 'View1Ctrl'
        }).
            when('/buttonView', {
                templateUrl: 'buttonView/buttonView.html',
                controller: 'buttonViewCtrl'
        });
    }])

.controller('View1Ctrl', [ '$scope', function($scope) {
    $scope.name = '';
    $scope.names = [{name:"Chris"}, {name:"Calvin"}];
    $scope.addName = function() {
    $scope.names.push( {'name':$scope.name} );
    $scope.name = '';
    };
    }])

.controller('buttonViewCtrl', ['$scope', '$location', function($scope, $location) {
    $scope.go = function( path ) {
        $location.path( path );
 };
}]);    

view2.html

<p> What is going on </p>

view2.js:

'use strict';

angular.module('myApp.view2', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/view2', {
    templateUrl: 'view2/view2.html',
    controller: 'View2Ctrl'
  });
}])

.controller('View2Ctrl', [function() {

}]);

buttonView.html

<a href = "#"> Home </a> 
<a href = "#"> About us </a> 
<a href = "#"> Why </a>

buttonView.js

'use strict';

angular.module('myApp.buttonView', ['ngRoute'])
    .config(['$routeProvider', function($routeProvider) {
        $routeProvider.when('/buttonView', {
            templateUrl: 'buttonView/buttonView.html'
        });
    }])

【问题讨论】:

  • 你不需要声明两个ng-controller

标签: javascript angularjs


【解决方案1】:

您在该页面上多次声明ngController。这不会创建一个共享实例,而是创建许多独立实例。将您的元素包装在一个容器中,并在其上使用ngController

<div ng-controller="buttonViewCtrl">
    <button type="button" class="btn btn-default btn-lg navButton" aria-label="Center Align" ng-click="nav()">
        <span class="glyphicon glyphicon-align-justify" aria-hidden="true">    </span>
    </button>

    <div> My name is {{buttonDisplay}} </div>
</div>

【讨论】:

    【解决方案2】:

    你不必有多个控制器定义,它必须像这样包装

    <div ng-controller="buttonViewCtrl">
    
       <button type="button" class="btn btn-default btn-lg navButton" aria-label="Center Align" ng-click="nav">
          <span class="glyphicon glyphicon-align-justify" aria-hidden="true"></span>
       </button>
    
       My name is {{buttonDisplay}}
    </div>
    

    【讨论】:

      【解决方案3】:

      这是您想要的功能吗:plunkr

      相关代码:

      <button type="button" class="btn btn-default btn-lg navButton" aria-label="Center Align" ng-click="nav()">
        <span class="glyphicon glyphicon-align-justify" aria-hidden="true"></span>
      </button>
      <div> My name is {{buttonDisplay}} </div>
      

      我不确定您到底遇到了什么问题,但这可能是不必要的 ng-controller 指令。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-01
        • 2016-07-08
        • 2015-08-05
        • 2015-10-29
        • 2016-07-15
        相关资源
        最近更新 更多