javascript - AngularJs代码效率mvc框架

标签 javascript angularjs model-view-controller

我刚刚接触 AngularJs,还没有理解整个事情,我有很多问题,如下:

  1. 我想仅在 onclick 期间点击 Controller 上的代码,这可能吗?
  2. 或者这是不可能的,因为 Controller 总是在 View 加载时加载?
  3. 创建一个仅在单击按钮时才会触发的函数的最佳方法是什么?而且不是每次页面加载时。 (AngularJs方式)

这是我的 html:

<ion-view view-title="Account Fixer">
      <ion-content class="padding">
        <div class="list">
            <label class="item item-input item-stacked-label">
                <span class="input-label">IrId</span>
                <input type="text" placeholder="irid" value="{{irid}}" disabled>
            </label>
            <label class="item item-input item-stacked-label">
                <span class="input-label">Email</span>
                <input type="text" placeholder="Enter New Email" value="{{email}}">
            </label>
            <button class="item button button-block button-positive" ng-click="postForm()">Fix Issue</button>
        </div>
      </ion-content>
    </ion-view>

这是我的 Controller :

.controller('TestCtrl',function(Test,$scope, $http, $stateParams){

$scope.email = Test.get('AB12345').email;
$scope.irid = Test.get('AB12345').irid;

//I want to hit this during onclick ONLY
    $scope.postForm = function(dataForm){

    var encodedString = 'action=' + 
            encodeURIComponent("QA_fixEmail") +
            '&irid=' +
            encodeURIComponent(Acct.acctData().irid) + 
            '&email=' +
            encodeURIComponent(dataForm.email) + 
            '&password=' +
            encodeURIComponent("abcd1234");
    $http({
        method: 'POST',
        url: 'someservice',
        data: encodedString,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    })
    .success(function(data,status){
        console.log(status);
    })
    .error(function(data, status){
        console.log(status);
    })
}
})

最佳答案

问:是否可以...仅在 onclick 期间[点击某个函数]?

答:是的,当然。只需使用 ngClick

以下是一些示例:

AngularJS Events

请注意,一个链接使用 ng-click,另一个链接使用 ngClick。这些是等效的:

AngularJS, ng-click vs ng:click vs ngClick

关于javascript - AngularJs代码效率mvc框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37312916/

相关文章:

jquery - 单击按钮即可调用两个不同的 Controller 方法(操作)

ruby-on-rails - ViewModel和Controller有什么区别?

javascript - 如何在 Backbone.js 中设置 View 的 id?

javascript - 如何在一页上运行 javascript/Canvas 脚本两次?

javascript - jquery onclick 运行两次

javascript - 如何在 Bootstrap timepicker 上设置默认时间

javascript - 如何在angularjs中读取数组对象

javascript - Controller 中的分配未传递到服务

javascript - 使用Ajax方法检索数据时显示加载

php - 如何在 AngularJS 应用程序中使用 PHP/Mysql 生成的 json