如何在父级中访问模板控件值

时间:2015-06-24 04:58:54

标签: angularjs

我们有控制器,它有ng-include来加载外部html。现在我想访问父母的控制值。

我的模板是MainMenuCreate

@{

    Layout = null;
}
<div >
<h2>{{Headtitle}}</h2>
<p>
    <div style="width:200px;">MainMenuId :</div>
     <input type="text" name="MainMenuId" id="MainMenuId" data-ng-model="MainMenuId" />
</p>
<p>
    <div style="width:200px;">MainMenu :</div>
     <input type="text" name="MainMenu" id="MainMenu" data-ng-model="MainMenu" />
</p>
<p>
    <div style="width:200px;">PageTitle :</div>
     <input type="text" name="PageTitle" id="PageTitle" data-ng-model="PageTitle" />
</p>

 <input type="button" name="btnSave" id="btnSave" value="Save" ng-click="Save()" />

我的主页是

<div data-ng-app="MainMenuModule">
    <div data-ng-controller="MainMenuController">
    <div data-ng-include="'MainMenuCreate'" >
     </div>
      {{MainMenuId}}
    </div>

</div>

和控制器是

app.controller('MainMenuController', function ($scope, MainMenuService) {     

    $scope.Save = function () {            
        var MainMenuRecord =
            {
                MainMenuId: $scope.MainMenuId,
                MainMenu: $scope.MainMenu,
                PageTitle: $scope.PageTitle

            };
        var promisePost = MainMenuService.post(MainMenuRecord);
        promisePost.then(function (pl) {
            $scope.MainMenu = pl.data.MainMenu;
            loadRecords();
        }, function (err) {
            console.log("Err" + err);
        });
    }

});

当我调用这个save方法时,$ scope.MainMenuId显示未定义的

1 个答案:

答案 0 :(得分:0)

<div style="width:200px;">MainMenuId :</div>
    <input type="text" name="MainMenuId" id="MainMenuId" data-ng-model="obj.MainMenuId" />
<div style="width:200px;">MainMenu :</div>
    <input type="text" name="MainMenu" id="MainMenu" data-ng-model="obj.MainMenu" />
<div style="width:200px;">PageTitle :</div>
   <input type="text" name="PageTitle" id="PageTitle" data-ng-model="obj.PageTitle" />
</div>

<input type="button" name="btnSave" id="btnSave" value="Save" ng-click="Save(obj)" />

app.controller('MainMenuController', function ($scope, MainMenuService)  {     
$scope.obj = {};
$scope.Save = function (obj) {            
    var MainMenuRecord =
        {
            MainMenuId: obj.MainMenuId,
            MainMenu: obj.MainMenu,
            PageTitle: obj.PageTitle

        };
    var promisePost = MainMenuService.post(MainMenuRecord);
    promisePost.then(function (pl) {
        $scope.MainMenu = pl.data.MainMenu;
        loadRecords();
    }, function (err) {
        console.log("Err" + err);
    });
}

});