改变模型的价值没有任何影响

时间:2015-09-20 12:00:43

标签: angularjs angularjs-scope

以下是app.js

angular.module('mobApp', ['ionic', 'mobApp.controllers', 'mobApp.services', 'ngCordova'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {

  });
})

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  .state('listitems', {
      url: '/listitems',
      templateUrl: 'templates/listitems.html'
    })

  .state('publishmsg/form/new-message', {
      url: '/publishmsg/form/new-message',
      templateUrl: 'templates/publish-msg/publishmsg-form.html',
      controller: 'publishMessageFormController'
  })  

  .state('publishmsg/form/add-tags', {
      url: '/publishmsg/form/add-tags',
      templateUrl: 'templates/publish-msg/publishmsg-form-tags.html',
      controller: 'publishMessageFormController'
  })    

  .state('publishmsg/form/add-location', {
      url: '/publishmsg/form/add-location',
      templateUrl: 'templates/publish-msg/publishmsg-form-location.html',
      controller: 'publishMessageFormController'
  });

   $urlRouterProvider.otherwise('/listitems');
});

以下是publishMessageFormController

angular.module("mobApp.controllers",['ionic','ngTagsInput','google.places'])
.controller("publishMessageFormController",function($scope, $http, $location, $ionicLoading, $cordovaToast, $compile, deviceStatus, $cordovaImagePicker, $ionicHistory){

$scope.testModel = "Hello";

$scope.gotoAddTag = function()
  {
    $scope.testModel = "Hi";
    $location.path('publishmsg/form/add-tags');
  }

以下是publishmsg-form-tags.html

<ion-view view-title="Add Tags">
  <ion-nav-buttons side="right">
    <a class="button button-icon icon ion-android-done" href="#" ng-click="tagSelectionDone()"></a>
  </ion-nav-buttons>
  <ion-content class="padding"> 
            <input type="text" ng-model="testModel" />     
  </ion-content>
</ion-view>

即使在将gotoAddTag()分配给testModel后,问题仍在Hi内,但是Hello

1 个答案:

答案 0 :(得分:0)

这看起来像是范围层次结构的误解。您应该将模型包装在父对象中,以确保更改了正确的密钥。例如:

$scope.model = {
    testModel: 'Hello'
};

然后

$scope.model.testModel = 'Hi';

基本上,读取和写入的$ scope在两种情况下都是不同的。你可以在这里阅读更多。

https://github.com/angular/angular.js/wiki/Understanding-Scopes