【问题标题】:AngularJs code efficiency mvc frameworkAngularJs代码效率mvc框架
【发布时间】:2016-05-19 02:21:13
【问题描述】:

我只是 AngularJs 的新手,还没有完全理解,我有很多问题,这些问题如下:

  1. 我只想在 onclick 期间点击控制器上的代码,可以吗?
  2. 或者因为控制器总是在视图加载时加载而不可能?
  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('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);
    })
}
})

【问题讨论】:

  • 到目前为止您所展示的内容应该可以正常工作。它在什么方面不起作用?
  • 它正在工作,我想要实现的是,仅在我的 html 按钮单击期间点击 $scope.postForm

标签: javascript angularjs model-view-controller


【解决方案1】:

问:是否有可能......仅在 onclick 期间 [点击功能]?

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

这里有一些例子:

AngularJS Events

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

AngularJS, ng-click vs ng:click vs ngClick

【讨论】:

  • 保罗先生,但是为什么当我在代码上放置一个断点时,即使我还没有点击按钮,它也会命中 $scope.postForm = function()
  • 因为 DEFINIING 一个 JS 函数(在第一次解析代码时完成)不同于 EXECUTING 一个函数(仅在它被调用时完成 - 例如通过点击事件)。把你的断点 INSIDE postForm 你应该明白我的意思。 '希望有帮助:)
  • 所以这意味着它只初始化了函数?但不一定执行我的解释对吗?
  • 是的,完全正确。顺便说一句:您的代码看起来不错,看起来您已经熟悉 ngClick ;)
  • 是的,我在其他页面上使用过 ngClick。谢谢顺便说一句,我认为在命中期间,它正在执行......谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
相关资源
最近更新 更多