更改$ watch中的范围值并不反映在html中

时间:2016-06-21 23:05:34

标签: javascript angularjs

HTML

 <nav-Header></nav-Header>
 <div ng-controller="loginController">
<div class="col-xs-8 col-sm-4 col-md-4 col-sm-offset-4 col-md-offset-4">
    <div class="input-group">
        <input type="text" class="form-control"
            placeholder="Search for a docType"
            typeahead-on-select="onSelect($item, $model, $label)"
            ng-model="selected" /> <span class="input-group-addon"
            ng-click="alert()"> <i
            class="glyphicon glyphicon-triangle-bottom"></i>
        </span>
    </div>

    sme= {{sme}}
</div>

Controller - 对后端进行嵌套REST调用

(function() {
  'use strict';

  var ezControllers = angular.module('login.controller', ['com.doc.ui.components.message.services',
    'com.doc.ui.components.message.constants'
  ]);

  ezControllers.controller('loginController', [
    '$state',
    '$scope',
    '$log',
    '$window',
    'messageService',
    'messageCodesConstant',
    '$http',
    '$location',
    '$timeout',
    '$uibModal',
    '$interval',
    '$rootScope',
    'com.doc.ui.loginService',
    'com.doc.ui.dropdownService',
    function($state, $scope, $log, $window, messageService,
      messageCodesConstant, $http, $location, $timeout,
      $uibModal, $interval, $rootScope, loginService, dropdownService) {

      var self = this;

      // flag to keep track of service operation
      this.callInProgress = false;
      $scope.docList = null;

      $scope.something = null;

      // $scope.docList = "hi there";
      $scope.states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
      /**
       * Authenticate the user
       */
      var call = $scope.authenticate = function(
        inUserName, inUserPassword) {



        // authenticate the user using service, and
        // if all good,
        // then forward to the home page
        self.callInProgress = true;
        loginService.authenticate(inUserName,
            inUserPassword)
          .then(
            // on successful
            // authentication
            function(userProfile) {




              dropdownService.getDocTypeList().then(
                // on successful
                // authentication
                function(docTypeList) {

                  // go to Search page
                  // directly


                  $log.log('docTypeList in controller:' + angular
                    .toJson(docTypeList.recordsList, true));

                  angular.forEach(docTypeList.recordsList, function(docType) {
                    console.log(docType.name);


                  })

                  // $scope.docList=docTypeList.recordsList;
                  $log.log('scope.docList:' + docTypeList.recordsList[0].name);

                  //setting new value
                  $scope.something = 'updated valuee';


                  $scope.$watch('something', function(newVal, oldVal) {
                    $log.log('newvalue: ' + newVal + ':oldval:' + oldVal);
                    $scope.sme = newVal;

                  });

                  $state
                    .go('home');

                },
                // on getting error
                function(errorMsg) {
                  $log
                    .error('Error in userlogin controller: ' + errorMsg);
                  messageService
                    .error(
                      messageCodesConstant.LOGIN_FAILURE,
                      "doc type list service Failure! Please try again. Error: " + errorMsg);
                },
                // update from service -
                // optional
                function(update) {
                  $log
                    .log('Update in userlogin controller: ' + update);
                }).finally(function() {
                $log.log('Service call to authenticate user ended.');
                self.callInProgress = false;
              });





            },
            // on getting error
            function(errorMessage) {
              $log
                .error('Error in userlogin controller: ' + errorMessage);
              messageService
                .error(
                  messageCodesConstant.LOGIN_FAILURE,
                  "Login Failure! Please try again. Error: " + errorMessage);
            },
            // update from service -
            // optional
            function(update) {
              $log
                .log('Update in userlogin controller: ' + update);
            }).finally(function() {
            $log.log('Service call to authenticate user ended.');
            self.callInProgress = false;
          });

      };



    }
  ]);


})();

当$ scope.something改变时,我将新值赋给$ scope.sme(参考watch方法)。 html中的$ scope.sme没有任何值。但是,当我记录值时,我在那里看到更新的值。我发现了这篇文章,但dint了解它如何应用于我的案例 - change $scope value in controller dose not reflect in view

0 个答案:

没有答案