我对范围变量有疑问。以下是案例;
情况 1:使用 ng-controller 标签在 html 中指定 Controller 名称。在这种情况下,警报会打印输入变量。
<ion-view view-title=“TEST”>
<ion-content ng-controller=‘TestController’>
<input name="aaa" id="dsds" ng-model="aaa" type="text"/>
<h3>{{aaa}}</h3>
<button class="button" ng-click=“test()”>Test</button>
</ion-content>
</ion-view>
//CONTROLLERS//
$scope.test = function(sql){
var x = $scope.aaa;
alert(x);
};
案例2:当我在config中定义 Controller 时,以下警报“未定义”
state('app.test’, {
url: ‘/test’,
views: {
'menuContent': {
controller: 'TestController',
templateUrl: 'templates/programs/test.html'
}
}
})
<ion-view view-title=“TEST”>
<ion-content>
<input name="aaa" id="dsds" ng-model="aaa" type="text"/>
<h3>{{aaa}}</h3>
<button class="button" ng-click=“test()”>Test</button>
</ion-content>
</ion-view>
//CONTROLLERS//
$scope.test = function(sql){
var x = $scope.aaa;
alert(x);
};
在情况 2 中,如果我将 ng-model =“aaa” 更改为 ng-model=“$parent.aaa”,则警报会获取值。
所以我的问题是在应用程序配置部分中给出 Controller 名称和 ng-controller 属性之间有什么区别
谢谢
最佳答案
这种情况仅适用于 Ionic。因为 Ionic 是基于 Angular Directive 的框架。所有指令都已经有 self Controller ,因此如果您声明 ng-model="aaa" 那么它就是与 self Controller $scope 变量绑定(bind)的变量。
您还可以在 github 上看到 Ionic 并找到 .directive('ionView' 您看到内部指令已经定义了一个 Controller 。我认为这是可能的情况。
关于javascript - 应用程序配置中的 Angularjs Controller 名称与 ng-controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33043443/