Angularjs 未显示向模型添加新值
这里是app.js的Angularjs 未显示向模型添加新值,angularjs,ionic-framework,ionic,Angularjs,Ionic Framework,Ionic,这里是app.js的 angular.module('mobApp', ['ionic', 'mobApp.controllers', 'mobApp.services', 'ngCordova']) .run(function($ionicPlatform) { $ionicPlatform.ready(function() { }); }) .config(function($stateProvider, $urlRouterProvider) { $stateProvid
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.finalPubFormModal = {};
$scope.geoSelectionDone = function()
{
$scope.finalPubFormModal.location = "fObj";
console.log("Log1"+JSON.stringify($scope.finalPubFormModal));
}
$scope.submitNewPublishMessage = function()
{
console.log("Log2"+JSON.stringify($scope.finalPubFormModal));
}
以下是publishmsg表单位置.html:
<ion-view view-title="Search Location">
<ion-nav-buttons side="right">
<button class="button button-icon icon ion-android-done" ng-disabled="npubmUsrLocForm.form.$invalid" ng-click="geoSelectionDone()"></button>
</ion-nav-buttons>
<ion-content>
<form name="npubmUsrLocForm.form" class="css-form" novalidate>
<div class="list">
<label class="item item-input">
<input placeholder="Type location name" type="text" ng-model="userCustmLocModel.location" ng-minlength="3" required/>
</label>
</div>
</form>
</ion-content>
</ion-view>
问题出在控制器内部
log1
打印新的位置
值,但log2没有我认为原因是每个页面都有一个单独的控制器实例。您可以尝试通过服务共享数据。差不多
angular.module('mobApp.services').service('finalPub', function() {
this.finalPubFormModal = {};
this.geoSelectionDone = function(value)
{
this.finalPubFormModal.location = value;
}
});
然后将该服务包括在控制器中
.controller("publishMessageFormController",function($scope, $http, $location, $ionicLoading, $cordovaToast, $compile, deviceStatus, $cordovaImagePicker, $ionicHistory,finalPub){ ...
$scope.geoSelectionDone = function()
{
finalPub.geoSelectionDone("fObj");
}
然后使用控制器中的服务
.controller("publishMessageFormController",function($scope, $http, $location, $ionicLoading, $cordovaToast, $compile, deviceStatus, $cordovaImagePicker, $ionicHistory,finalPub){ ...
$scope.geoSelectionDone = function()
{
finalPub.geoSelectionDone("fObj");
}
但在app.js中,你可以看到我引用的所有页面都是同一个控制器,这是真的,但据我回忆,将为每个路由创建一个新的控制器实例-为了在OOP中给你一个并行,可以这样想:你定义了一个类publishMessageFormController,然后创建了var controller1=new publishMessageFormController(),var controller2=new publishMessageFormController()-如果在controller1中设置了某些内容,则controller2中不会发生任何更改。